Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 870108a

Browse filesBrowse 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 <rtrott@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent e2b8393 commit 870108a
Copy full SHA for 870108a

File tree

Expand file treeCollapse file tree

3 files changed

+9
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+9
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/console.markdown‎

Copy file name to clipboardExpand all lines: doc/api/console.markdown
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
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

Collapse file

‎lib/console.js‎

Copy file name to clipboardExpand all lines: lib/console.js
+4-3Lines changed: 4 additions & 3 deletions
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

Collapse file

‎test/parallel/test-console.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-console.js
+4-4Lines changed: 4 additions & 4 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.