diff options
author | Brian White <mscdex@mscdex.net> | 2018-03-11 23:48:01 -0400 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2018-03-15 12:57:35 -0400 |
commit | 3c61b87e5918441c9d14641e328584e3ce184c98 (patch) | |
tree | 8ece20ea45e925f63888a45e9c4275e832c0e32b | |
parent | 11b6c0de4114fa499bcffbda91d8cea9d3bdea03 (diff) | |
download | node-new-3c61b87e5918441c9d14641e328584e3ce184c98.tar.gz |
assert: improve assert()/assert.ok() performance
PR-URL: https://github.com/nodejs/node/pull/19292
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
-rw-r--r-- | benchmark/assert/ok.js | 20 | ||||
-rw-r--r-- | lib/assert.js | 10 |
2 files changed, 24 insertions, 6 deletions
diff --git a/benchmark/assert/ok.js b/benchmark/assert/ok.js new file mode 100644 index 0000000000..849be65539 --- /dev/null +++ b/benchmark/assert/ok.js @@ -0,0 +1,20 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e9] +}); + +function main({ n }) { + var i; + bench.start(); + for (i = 0; i < n; ++i) { + if (i % 2 === 0) + assert(true); + else + assert(true, 'foo bar baz'); + } + bench.end(n); +} diff --git a/lib/assert.js b/lib/assert.js index 797252afc0..ccc1de3a25 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -206,13 +206,11 @@ function getErrMessage(call) { } } -function innerOk(args, fn) { - var [value, message] = args; - +function innerOk(fn, argLen, value, message) { if (!value) { let generatedMessage = false; - if (args.length === 0) { + if (argLen === 0) { generatedMessage = true; message = 'No value argument passed to `assert.ok()`'; } else if (message == null) { @@ -253,7 +251,7 @@ function innerOk(args, fn) { // Pure assertion tests whether a value is truthy, as determined // by !!value. function ok(...args) { - innerOk(args, ok); + innerOk(ok, args.length, ...args); } assert.ok = ok; @@ -563,7 +561,7 @@ assert.ifError = function ifError(err) { // Expose a strict only variant of assert function strict(...args) { - innerOk(args, strict); + innerOk(strict, args.length, ...args); } assert.strict = Object.assign(strict, assert, { equal: assert.strictEqual, |