From 3b40fafca131c4a7b27e178050734b5f0f0ef4b8 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sat, 27 Dec 2025 10:16:56 +0300 Subject: [PATCH 1/8] benchmark: add benchmark utility for isNativeError function --- benchmark/util/is-native-error.js | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 benchmark/util/is-native-error.js diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-native-error.js new file mode 100644 index 00000000000000..480461670b7ac8 --- /dev/null +++ b/benchmark/util/is-native-error.js @@ -0,0 +1,35 @@ +'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; + const arg = args[argument]; + + bench.start(); + for (let iteration = 0; iteration < n; iteration++) { + func(arg); + } + bench.end(n); +} From 5fcf423cf6d0bfc6007a54e899376f91ba9662f1 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sat, 27 Dec 2025 19:41:45 +0300 Subject: [PATCH 2/8] repair benchmark --- benchmark/util/is-native-error.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-native-error.js index 480461670b7ac8..29ff42c996bc1d 100644 --- a/benchmark/util/is-native-error.js +++ b/benchmark/util/is-native-error.js @@ -25,11 +25,15 @@ function main({ argument, version, n }) { const types = require('internal/util/types'); const func = { native: util, js: types }[version].isNativeError; - const arg = args[argument]; + + const testArgs = [args[argument], args[argument], args[argument]]; + let sum = 0; bench.start(); for (let iteration = 0; iteration < n; iteration++) { - func(arg); + const testArg = testArgs[iteration % 3]; + sum += func(testArg) ? 1 : 0; } bench.end(n); + if (sum < 0) console.log(sum); } From 9de3f6fecd0fe04dbaa7570ff56ac9ca6dd2b47c Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 28 Dec 2025 10:55:04 +0300 Subject: [PATCH 3/8] refine test arguments and measure elapsed time in isNativeError benchmark --- benchmark/util/is-native-error.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-native-error.js index 29ff42c996bc1d..86db9c29439585 100644 --- a/benchmark/util/is-native-error.js +++ b/benchmark/util/is-native-error.js @@ -26,14 +26,15 @@ function main({ argument, version, n }) { const func = { native: util, js: types }[version].isNativeError; - const testArgs = [args[argument], args[argument], args[argument]]; - let sum = 0; + const testArgs = [args.true, args.falsePrimitive, args.falseObject]; bench.start(); + const start = performance.now(); for (let iteration = 0; iteration < n; iteration++) { - const testArg = testArgs[iteration % 3]; - sum += func(testArg) ? 1 : 0; + const testArg = testArgs[iteration % testArgs.length]; + func(testArg); } + const end = performance.now(); bench.end(n); - if (sum < 0) console.log(sum); + console.log('Elapsed(ms):', (end - start).toFixed(3)); } From c4aaa5daee5a78e93f553d147db46139e7f84e8b Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 28 Dec 2025 14:38:40 +0300 Subject: [PATCH 4/8] simplify isNativeError test arguments and remove performance logging --- benchmark/util/is-native-error.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-native-error.js index 86db9c29439585..665c436bab80b4 100644 --- a/benchmark/util/is-native-error.js +++ b/benchmark/util/is-native-error.js @@ -11,8 +11,8 @@ const args = { const bench = common.createBenchmark( main, { - argument: ['true', 'falsePrimitive', 'falseObject'], - version: ['native', 'js'], + argument: ['true'], + version: ['native'], n: [1e6], }, { @@ -25,16 +25,11 @@ function main({ argument, version, n }) { const types = require('internal/util/types'); const func = { native: util, js: types }[version].isNativeError; - - const testArgs = [args.true, args.falsePrimitive, args.falseObject]; + const arg = args[argument]; bench.start(); - const start = performance.now(); for (let iteration = 0; iteration < n; iteration++) { - const testArg = testArgs[iteration % testArgs.length]; - func(testArg); + func(arg); } - const end = performance.now(); bench.end(n); - console.log('Elapsed(ms):', (end - start).toFixed(3)); } From 23955ed3db9356af4044494083ebd03aaa59bb7a Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 30 Dec 2025 19:43:34 +0300 Subject: [PATCH 5/8] store result of isNativeError function for validation --- benchmark/util/is-native-error.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-native-error.js index 665c436bab80b4..788af69aa43e93 100644 --- a/benchmark/util/is-native-error.js +++ b/benchmark/util/is-native-error.js @@ -28,8 +28,10 @@ function main({ argument, version, n }) { const arg = args[argument]; bench.start(); + let result; for (let iteration = 0; iteration < n; iteration++) { - func(arg); + result = func(arg); } bench.end(n); + if (typeof result !== 'boolean') throw new Error('unexpected result'); } From f2e53f18a1eef5954c3c3f5605b9c03a8febd4c8 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 30 Dec 2025 19:57:06 +0300 Subject: [PATCH 6/8] add benchmark utility for isError function --- .../util/{is-native-error.js => is-error.js} | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) rename benchmark/util/{is-native-error.js => is-error.js} (60%) diff --git a/benchmark/util/is-native-error.js b/benchmark/util/is-error.js similarity index 60% rename from benchmark/util/is-native-error.js rename to benchmark/util/is-error.js index 788af69aa43e93..0dd5cac9bfe584 100644 --- a/benchmark/util/is-native-error.js +++ b/benchmark/util/is-error.js @@ -12,24 +12,20 @@ const bench = common.createBenchmark( main, { argument: ['true'], - version: ['native'], + version: ['instanceof', 'isError'], 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; const arg = args[argument]; + const func = version === 'isError' + ? Error.isError + : (v) => v instanceof Error; bench.start(); let result; - for (let iteration = 0; iteration < n; iteration++) { + for (let i = 0; i < n; i++) { result = func(arg); } bench.end(n); From 6b26012462c560d08c46391abbc20f986fadba81 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 30 Dec 2025 20:05:48 +0300 Subject: [PATCH 7/8] refactor benchmark utility for isError function to improve readability --- benchmark/util/is-error.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/benchmark/util/is-error.js b/benchmark/util/is-error.js index 0dd5cac9bfe584..076c1ec878ad60 100644 --- a/benchmark/util/is-error.js +++ b/benchmark/util/is-error.js @@ -14,14 +14,12 @@ const bench = common.createBenchmark( argument: ['true'], version: ['instanceof', 'isError'], n: [1e6], - } + }, ); function main({ argument, version, n }) { const arg = args[argument]; - const func = version === 'isError' - ? Error.isError - : (v) => v instanceof Error; + const func = version === 'isError' ? Error.isError : (v) => v instanceof Error; bench.start(); let result; From 8c79e293d0246ea61e368faff19121bdee01f01c Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 30 Dec 2025 20:49:09 +0300 Subject: [PATCH 8/8] add benchmark utility for isNativeError function --- benchmark/util/{is-error.js => is-native-error.js} | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) rename benchmark/util/{is-error.js => is-native-error.js} (61%) diff --git a/benchmark/util/is-error.js b/benchmark/util/is-native-error.js similarity index 61% rename from benchmark/util/is-error.js rename to benchmark/util/is-native-error.js index 076c1ec878ad60..788af69aa43e93 100644 --- a/benchmark/util/is-error.js +++ b/benchmark/util/is-native-error.js @@ -12,18 +12,24 @@ const bench = common.createBenchmark( main, { argument: ['true'], - version: ['instanceof', 'isError'], + version: ['native'], 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; const arg = args[argument]; - const func = version === 'isError' ? Error.isError : (v) => v instanceof Error; bench.start(); let result; - for (let i = 0; i < n; i++) { + for (let iteration = 0; iteration < n; iteration++) { result = func(arg); } bench.end(n);