Skip to content

Commit 870108a

Browse files
targosrvagg
authored andcommitted
console: sub-millisecond accuracy for console.time
This makes the output of console.timeEnd in line with major browsers. PR-URL: #3166 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent e2b8393 commit 870108a

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

doc/api/console.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Example:
9393
;
9494
}
9595
console.timeEnd('100-elements');
96-
// prints 100-elements: 262ms
96+
// prints 100-elements: 225.438ms
9797

9898
### console.trace(message[, ...])
9999

lib/console.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Console.prototype.dir = function(object, options) {
5656

5757

5858
Console.prototype.time = function(label) {
59-
this._times.set(label, Date.now());
59+
this._times.set(label, process.hrtime());
6060
};
6161

6262

@@ -65,8 +65,9 @@ Console.prototype.timeEnd = function(label) {
6565
if (!time) {
6666
throw new Error('No such label: ' + label);
6767
}
68-
var duration = Date.now() - time;
69-
this.log('%s: %dms', label, duration);
68+
const duration = process.hrtime(time);
69+
const ms = duration[0] * 1000 + duration[1] / 1e6;
70+
this.log('%s: %sms', label, ms.toFixed(3));
7071
};
7172

7273

test/parallel/test-console.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ assert.notEqual(-1, strings.shift().indexOf('foo: [Object]'));
6868
assert.equal(-1, strings.shift().indexOf('baz'));
6969
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
7070
strings.shift().split('\n').shift());
71-
assert.ok(/^label: \d+ms$/.test(strings.shift().trim()));
72-
assert.ok(/^__proto__: \d+ms$/.test(strings.shift().trim()));
73-
assert.ok(/^constructor: \d+ms$/.test(strings.shift().trim()));
74-
assert.ok(/^hasOwnProperty: \d+ms$/.test(strings.shift().trim()));
71+
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
72+
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
73+
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));
74+
assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim()));
7575
assert.equal(strings.length, 0);

0 commit comments

Comments
 (0)