node/benchmark/util/diff.js
Rafael Gonzaga 62c62056ca
benchmark: calibrate length of util.diff
500 + 1000 already cover the curve. 2000 doesn’t add
new qualitative insight — it just extends the same curve
further down (another ~3–4× slowdown).

According to https://github.com/nodejs/performance/issues/186
this benchmark takes one minute to conclude a single run.
This should fix it.

PR-URL: https://github.com/nodejs/node/pull/59588
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2025-08-24 20:57:38 +00:00

44 lines
1.1 KiB
JavaScript

'use strict';
const util = require('util');
const common = require('../common');
const bench = common.createBenchmark(main, {
n: [1e3],
length: [500, 1000],
scenario: ['identical', 'small-diff', 'medium-diff', 'large-diff'],
});
function main({ n, length, scenario }) {
const actual = Array.from({ length }, (_, i) => `${i}`);
let expected;
switch (scenario) {
case 'identical': // 0% difference
expected = Array.from({ length }, (_, i) => `${i}`);
break;
case 'small-diff': // ~5% difference
expected = Array.from({ length }, (_, i) => {
return Math.random() < 0.05 ? `modified-${i}` : `${i}`;
});
break;
case 'medium-diff': // ~25% difference
expected = Array.from({ length }, (_, i) => {
return Math.random() < 0.25 ? `modified-${i}` : `${i}`;
});
break;
case 'large-diff': // ~100% difference
expected = Array.from({ length }, (_, i) => `modified-${i}`);
break;
}
bench.start();
for (let i = 0; i < n; i++) {
util.diff(actual, expected);
}
bench.end(n);
}