Skip to content

Commit

Permalink
assert: improve assert()/assert.ok() performance
Browse files Browse the repository at this point in the history
PR-URL: nodejs#19292
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
mscdex committed Mar 15, 2018
1 parent 11b6c0d commit 3c61b87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 20 additions & 0 deletions benchmark/assert/ok.js
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 4 additions & 6 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3c61b87

Please sign in to comment.