Skip to content
39 changes: 39 additions & 0 deletions benchmark/util/is-native-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const common = require('../common');

const args = {
true: new Error('test'),
falsePrimitive: 42,
falseObject: { foo: 'bar' },
};

const bench = common.createBenchmark(
main,
{
argument: ['true', 'falsePrimitive', 'falseObject'],
version: ['native', 'js'],
n: [1e6],
},
{
flags: ['--expose-internals', '--no-warnings'],
},
);

function main({ argument, version, n }) {
const util = common.binding('util');
const types = require('internal/util/types');

const func = { native: util, js: types }[version].isNativeError;
Copy link
Member

@RafaelGSS RafaelGSS Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't recently deprecated?

### DEP0197: `util.types.isNativeError()`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is deprecated but still supported - we should still not regress on stable releases and this change ensures that @RafaelGSS

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, what's the point of this benchmark? I mean, if we see util/isNativeError is slower, what it means afterall? Which API will be directly impacted besides the deprecated API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comparison ensures that existing users will not experience performance drops or unexpected slowdowns until the API is completely removed. It also aims to provide a reference for future changes or alternatives.


const testArgs = [args[argument], args[argument], args[argument]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this? If I'm reading correctly, the elements are all equals.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to provide more arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With

const testArg = testArgs[iteration % 3];
func(testArg)

below, I'm still not sure I understand.

Copy link
Member Author

@mertcanaltin mertcanaltin Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it could be misleading for them all to be the same I changed them with different arguments.
9de3f6f

now "%3" alternative I use testArgs.length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the latest changes, I got a test failure because the benchmark was running with three configurations instead of the expected one or two. I’ve updated the code so each run uses only a single argument, matching the test infrastructure’s requirements. FYİ @RafaelGSS

let sum = 0;

bench.start();
for (let iteration = 0; iteration < n; iteration++) {
const testArg = testArgs[iteration % 3];
sum += func(testArg) ? 1 : 0;
}
bench.end(n);
if (sum < 0) console.log(sum);
}
Loading