Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

43
benchmark/util/diff.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
const util = require('util');
const common = require('../common');
const bench = common.createBenchmark(main, {
n: [1e3],
length: [1e3, 2e3],
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);
}

33
benchmark/util/format.js Normal file
View File

@ -0,0 +1,33 @@
'use strict';
const util = require('util');
const common = require('../common');
const inputs = {
'string': ['Hello, my name is %s', 'Fred'],
'string-2': ['Hello, %s is my name', 'Fred'],
'number': ['Hi, I was born in %d', 1989],
'replace-object': ['An error occurred %j', { msg: 'This is an error' }],
'unknown': ['hello %a', 'test'],
'no-replace': [1, 2],
'no-replace-2': ['foobar', 'yeah', 'mensch', 5],
'only-objects': [{ msg: 'This is an error' }, { msg: 'This is an error' }],
'many-%': ['replace%%%%s%%%%many%s%s%s', 'percent'],
'object-to-string': ['foo %s bar', { toString() { return 'bla'; } }],
'object-%s': ['foo %s bar', { a: true, b: false }],
};
const bench = common.createBenchmark(main, {
n: [1e6],
type: Object.keys(inputs),
});
function main({ n, type }) {
const [first, second] = inputs[type];
bench.start();
for (let i = 0; i < n; i++) {
util.format(first, second);
}
bench.end(n);
}

View File

@ -0,0 +1,65 @@
'use strict';
const common = require('../common');
const { getCallSites } = require('node:util');
const assert = require('node:assert');
const bench = common.createBenchmark(main, {
n: [1e6],
method: ['ErrorCallSites', 'ErrorCallSitesSerialized', 'CPP'],
});
function ErrorGetCallSites() {
const originalStackFormatter = Error.prepareStackTrace;
Error.prepareStackTrace = (_err, stack) => {
if (stack && stack.length > 1) {
// Remove node:util
return stack.slice(1);
}
return stack;
};
const err = new Error();
// With the V8 Error API, the stack is not formatted until it is accessed
err.stack; // eslint-disable-line no-unused-expressions
Error.prepareStackTrace = originalStackFormatter;
return err.stack;
}
function ErrorCallSitesSerialized() {
const callSites = ErrorGetCallSites();
const serialized = [];
for (let i = 0; i < callSites.length; ++i) {
serialized.push({
functionName: callSites[i].getFunctionName(),
scriptName: callSites[i].getFileName(),
lineNumber: callSites[i].getLineNumber(),
column: callSites[i].getColumnNumber(),
});
}
return serialized;
}
function main({ n, method }) {
let fn;
switch (method) {
case 'ErrorCallSites':
fn = ErrorGetCallSites;
break;
case 'ErrorCallSitesSerialized':
fn = ErrorCallSitesSerialized;
break;
case 'CPP':
fn = getCallSites;
break;
}
let lastStack = {};
bench.start();
for (let i = 0; i < n; i++) {
const stack = fn();
lastStack = stack;
}
bench.end(n);
// Attempt to avoid dead-code elimination
assert.ok(lastStack);
}

View File

@ -0,0 +1,43 @@
'use strict';
const common = require('../common');
const util = require('util');
const bench = common.createBenchmark(main, {
n: [5e3],
len: [1e2, 1e5],
type: [
'denseArray',
'sparseArray',
'mixedArray',
'denseArray_showHidden',
],
});
function main({ n, len, type }) {
let arr = Array(len);
let opts;
switch (type) {
case 'denseArray_showHidden':
opts = { showHidden: true };
arr = arr.fill('denseArray');
break;
case 'denseArray':
arr = arr.fill('denseArray');
break;
case 'sparseArray':
break;
case 'mixedArray':
for (let i = 0; i < n; i += 2)
arr[i] = i;
break;
default:
throw new Error(`Unsupported type ${type}`);
}
bench.start();
for (let i = 0; i < n; i++) {
util.inspect(arr, opts);
}
bench.end(n);
}

View File

@ -0,0 +1,23 @@
'use strict';
const util = require('util');
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [1e5],
showProxy: [0, 1],
isProxy: [0, 1],
});
function main({ n, showProxy, isProxy }) {
let proxyA = {};
let proxyB = () => {};
if (isProxy) {
proxyA = new Proxy(proxyA, { get: () => {} });
proxyB = new Proxy(proxyB, {});
}
bench.start();
for (let i = 0; i < n; i += 1)
util.inspect({ a: proxyA, b: proxyB }, { showProxy });
bench.end(n);
}

102
benchmark/util/inspect.js Normal file
View File

@ -0,0 +1,102 @@
'use strict';
const util = require('util');
const common = require('../common.js');
const opts = {
showHidden: { showHidden: true },
colors: { colors: true },
none: undefined,
};
const bench = common.createBenchmark(main, {
n: [8e4],
method: [
'Object',
'Object_empty',
'Object_deep_ln',
'String',
'String_complex',
'String_boxed',
'Date',
'Set',
'Error',
'Array',
'TypedArray',
'TypedArray_extra',
'Number',
],
option: Object.keys(opts),
});
function benchmark(n, obj, options) {
bench.start();
for (let i = 0; i < n; i += 1) {
util.inspect(obj, options);
}
bench.end(n);
}
function main({ method, n, option }) {
let obj;
const options = opts[option];
switch (method) {
case 'Object':
benchmark(n, { a: 'a', b: 'b', c: 'c', d: 'd' }, options);
break;
case 'Object_empty':
benchmark(n, {}, options);
break;
case 'Object_deep_ln':
if (options)
options.depth = Infinity;
obj = { first:
{ second:
{ third:
{ a: 'first',
b: 'second',
c: 'third',
d: 'fourth',
e: 'fifth',
f: 'sixth',
g: 'seventh' } } } };
benchmark(n, obj, options || { depth: Infinity });
break;
case 'String':
benchmark(n, 'Simple string', options);
break;
case 'String_complex':
benchmark(n, 'This string\nhas to be\tescaped!', options);
break;
case 'String_boxed':
benchmark(n, new String('string'), options);
break;
case 'Date':
benchmark(n, new Date(), options);
break;
case 'Set':
obj = new Set([5, 3]);
benchmark(n, obj, options);
break;
case 'Error':
benchmark(n, new Error('error'), options);
break;
case 'Array':
benchmark(n, Array(50).fill().map((_, i) => i), options);
break;
case 'TypedArray':
obj = new Uint8Array(Array(50).fill().map((_, i) => i));
benchmark(n, obj, options);
break;
case 'TypedArray_extra':
obj = new Uint8Array(Array(50).fill().map((_, i) => i));
obj.foo = 'bar';
obj[Symbol('baz')] = 5;
benchmark(n, obj, options);
break;
case 'Number':
benchmark(n, 0, options);
break;
default:
throw new Error(`Unsupported method "${method}"`);
}
}

View File

@ -0,0 +1,59 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const groupedInputs = {
group_common: ['undefined', 'utf8', 'utf-8', 'base64',
'binary', 'latin1', 'ucs2'],
group_upper: ['UTF-8', 'UTF8', 'UCS2',
'UTF16LE', 'BASE64', 'UCS2'],
group_uncommon: ['foo'],
group_misc: ['', 'utf16le', 'hex', 'HEX', 'BINARY'],
};
const inputs = [
'', 'utf8', 'utf-8', 'UTF-8', 'UTF8', 'Utf8',
'ucs2', 'UCS2', 'utf16le', 'UTF16LE',
'binary', 'BINARY', 'latin1', 'base64', 'BASE64',
'hex', 'HEX', 'foo', 'undefined',
];
const bench = common.createBenchmark(main, {
input: inputs.concat(Object.keys(groupedInputs)),
n: [1e6],
}, {
flags: '--expose-internals',
});
function getInput(input) {
switch (input) {
case 'group_common':
return groupedInputs.group_common;
case 'group_upper':
return groupedInputs.group_upper;
case 'group_uncommon':
return groupedInputs.group_uncommon;
case 'group_misc':
return groupedInputs.group_misc;
case 'undefined':
return [undefined];
default:
return [input];
}
}
function main({ input, n }) {
const { normalizeEncoding } = require('internal/util');
const inputs = getInput(input);
let noDead = '';
bench.start();
for (let i = 0; i < n; ++i) {
for (let j = 0; j < inputs.length; ++j) {
noDead = normalizeEncoding(inputs[j]);
}
}
bench.end(n);
assert.ok(noDead === undefined || noDead.length > 0);
}

View File

@ -0,0 +1,23 @@
'use strict';
const { createBenchmark } = require('../common.js');
const path = require('node:path');
const fs = require('node:fs');
const util = require('node:util');
const assert = require('node:assert');
const bench = createBenchmark(main, {
n: 3e4,
});
const env = fs.readFileSync(path.resolve(__dirname, '../fixtures/valid.env'), 'utf-8');
function main({ n }) {
let noDead;
bench.start();
for (let i = 0; i < n; i++) {
noDead = util.parseEnv(env);
}
bench.end(n);
assert(noDead !== undefined);
}

View File

@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const bench = common.createBenchmark(main, {
n: [1e5],
}, { flags: ['--expose-internals'] });
function main({ n, type }) {
const PriorityQueue = require('internal/priority_queue');
const queue = new PriorityQueue();
bench.start();
for (let i = 0; i < n; i++)
queue.insert(Math.random() * 1e7 | 0);
for (let i = 0; i < n; i++)
queue.shift();
bench.end(n);
}

View File

@ -0,0 +1,33 @@
'use strict';
const common = require('../common');
const bench = common.createBenchmark(main, {
n: [5e6],
pos: ['start', 'middle', 'end'],
size: [10, 100, 500],
}, { flags: ['--expose-internals'] });
function main({ n, pos, size }) {
const { spliceOne } = require('internal/util');
const arr = new Array(size);
arr.fill('');
let index;
switch (pos) {
case 'end':
index = size - 1;
break;
case 'middle':
index = Math.floor(size / 2);
break;
default: // start
index = 0;
}
bench.start();
for (let i = 0; i < n; i++) {
spliceOne(arr, index);
arr.push('');
}
bench.end(n);
}

View File

@ -0,0 +1,43 @@
'use strict';
const common = require('../common.js');
const { styleText } = require('node:util');
const assert = require('node:assert');
const bench = common.createBenchmark(main, {
messageType: ['string', 'number', 'boolean', 'invalid'],
format: ['red', 'italic', 'invalid'],
validateStream: [1, 0],
n: [1e3],
});
function main({ messageType, format, validateStream, n }) {
let str;
switch (messageType) {
case 'string':
str = 'hello world';
break;
case 'number':
str = 10;
break;
case 'boolean':
str = true;
break;
case 'invalid':
str = undefined;
break;
}
bench.start();
for (let i = 0; i < n; i++) {
let colored = '';
try {
colored = styleText(format, str, { validateStream });
assert.ok(colored); // Attempt to avoid dead-code elimination
} catch {
// eslint-disable no-empty
}
}
bench.end(n);
}

View File

@ -0,0 +1,42 @@
'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'windows-1252', 'iso-8859-3'],
ignoreBOM: [0, 1],
fatal: [0, 1],
len: [256, 1024 * 16, 1024 * 128],
n: [1e3],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
});
function main({ encoding, len, n, ignoreBOM, type, fatal }) {
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
let buf;
switch (type) {
case 'SharedArrayBuffer': {
buf = new SharedArrayBuffer(len);
break;
}
case 'ArrayBuffer': {
buf = new ArrayBuffer(len);
break;
}
case 'Buffer': {
buf = Buffer.allocUnsafe(len);
break;
}
}
bench.start();
for (let i = 0; i < n; i++) {
try {
decoder.decode(buf);
} catch {
// eslint-disable no-empty
}
}
bench.end(n);
}

View File

@ -0,0 +1,50 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
len: [32, 256, 1024, 1024 * 8],
n: [1e6],
type: ['one-byte-string', 'two-byte-string', 'ascii'],
op: ['encode', 'encodeInto'],
});
function main({ n, op, len, type }) {
const encoder = new TextEncoder();
let base = '';
switch (type) {
case 'ascii':
base = 'a';
break;
case 'one-byte-string':
base = '\xff';
break;
case 'two-byte-string':
base = 'ğ';
break;
}
const input = base.repeat(len);
if (op === 'encode') {
const expected = encoder.encode(input);
let result;
bench.start();
for (let i = 0; i < n; i++)
result = encoder.encode(input);
bench.end(n);
assert.deepStrictEqual(result, expected);
} else {
const expected = new Uint8Array(len);
const subarray = new Uint8Array(len);
const expectedStats = encoder.encodeInto(input, expected);
let result;
bench.start();
for (let i = 0; i < n; i++)
result = encoder.encodeInto(input, subarray);
bench.end(n);
assert.deepStrictEqual(subarray, expected);
assert.deepStrictEqual(result, expectedStats);
}
}

View File

@ -0,0 +1,49 @@
'use strict';
const common = require('../common');
const arrayBuffer = new ArrayBuffer();
const dataView = new DataView(arrayBuffer);
const uint8Array = new Uint8Array(arrayBuffer);
const int32Array = new Int32Array(arrayBuffer);
const args = {
ArrayBufferView: {
'true': dataView,
'false-primitive': true,
'false-object': arrayBuffer,
},
TypedArray: {
'true': int32Array,
'false-primitive': true,
'false-object': arrayBuffer,
},
Uint8Array: {
'true': uint8Array,
'false-primitive': true,
'false-object': int32Array,
},
};
const bench = common.createBenchmark(main, {
type: Object.keys(args),
version: ['native', 'js'],
argument: ['true', 'false-primitive', 'false-object'],
n: [1e6],
}, {
flags: ['--expose-internals', '--no-warnings'],
});
function main({ type, argument, version, n }) {
const util = common.binding('util');
const types = require('internal/util/types');
const func = { native: util, js: types }[version][`is${type}`];
const arg = args[type][argument];
bench.start();
for (let i = 0; i < n; i++) {
func(arg);
}
bench.end(n);
}