diff --git a/lib/console.js b/lib/console.js index ac8912f828ea6a..64d21a4266cdc6 100644 --- a/lib/console.js +++ b/lib/console.js @@ -49,7 +49,11 @@ exports.time = function(label) { exports.timeEnd = function(label) { - var duration = Date.now() - times[label]; + var time = times[label]; + if (!time) { + throw new Error('No such label: ' + label); + } + var duration = Date.now() - time; exports.log('%s: %dms', label, duration); }; diff --git a/test/simple/test-console.js b/test/simple/test-console.js index 3a73286def8031..d07ca50ae4a7be 100644 --- a/test/simple/test-console.js +++ b/test/simple/test-console.js @@ -50,3 +50,12 @@ assert.equal('foo bar hop\n', strings.shift()); assert.equal("{ slashes: '\\\\\\\\' }\n", strings.shift()); process.stderr.write('hello world'); + +assert.throws(function () { + console.timeEnd('no such label'); +}); + +assert.doesNotThrow(function () { + console.time('label'); + console.timeEnd('label'); +});