Skip to content

Commit f8193ab

Browse files
committed
timers: use uv_now instead of Date.now
This saves a few calls to gettimeofday which can be expensive, and potentially subject to clock drift. Instead use the loop time which uses hrtime internally. fixes nodejs#5497
1 parent f58eb8f commit f8193ab

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

lib/timers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var lists = {};
4545
// the main function - creates lists on demand and the watchers associated
4646
// with them.
4747
function insert(item, msecs) {
48-
item._idleStart = Date.now();
48+
item._idleStart = Timer.now();
4949
item._idleTimeout = msecs;
5050

5151
if (msecs < 0) return;
@@ -75,7 +75,7 @@ function listOnTimeout() {
7575

7676
debug('timeout callback %d', msecs);
7777

78-
var now = Date.now();
78+
var now = Timer.now();
7979
debug('now: %s', now);
8080

8181
var first;
@@ -165,7 +165,7 @@ exports.active = function(item) {
165165
if (!list || L.isEmpty(list)) {
166166
insert(item, msecs);
167167
} else {
168-
item._idleStart = Date.now();
168+
item._idleStart = Timer.now();
169169
L.append(list, item);
170170
}
171171
}
@@ -277,7 +277,7 @@ var Timeout = function(after) {
277277

278278
Timeout.prototype.unref = function() {
279279
if (!this._handle) {
280-
var now = Date.now();
280+
var now = Timer.now();
281281
if (!this._idleStart) this._idleStart = now;
282282
var delay = this._idleStart + this._idleTimeout - now;
283283
if (delay < 0) delay = 0;

lib/tls.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var stream = require('stream');
2828
var assert = require('assert').ok;
2929
var constants = require('constants');
3030

31+
var Timer = process.binding('timer_wrap').Timer;
32+
3133
var DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:' + // TLS 1.2
3234
'RC4:HIGH:!MD5:!aNULL:!EDH'; // TLS 1.0
3335

@@ -720,7 +722,7 @@ function onhandshakestart() {
720722

721723
var self = this;
722724
var ssl = self.ssl;
723-
var now = Date.now();
725+
var now = Timer.now();
724726

725727
assert(now >= ssl.lastHandshakeTime);
726728

src/timer_wrap.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class TimerWrap : public HandleWrap {
5151
constructor->InstanceTemplate()->SetInternalFieldCount(1);
5252
constructor->SetClassName(String::NewSymbol("Timer"));
5353

54+
NODE_SET_METHOD(constructor, "now", Now);
55+
5456
NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
5557
NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
5658
NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
@@ -163,6 +165,13 @@ class TimerWrap : public HandleWrap {
163165
MakeCallback(wrap->object_, ontimeout_sym, ARRAY_SIZE(argv), argv);
164166
}
165167

168+
static Handle<Value> Now(const Arguments& args) {
169+
HandleScope scope(node_isolate);
170+
171+
double now = static_cast<double>(uv_now(uv_default_loop()));
172+
return scope.Close(v8::Number::New(now));
173+
}
174+
166175
uv_timer_t handle_;
167176
};
168177

0 commit comments

Comments
 (0)