Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: improve assert()/assert.ok() performance #19292

Merged
merged 1 commit into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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