2025-11-04 03:54:35 +08:00
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import { runBenchmark } from "./utilities.js";
|
|
|
|
|
|
|
|
|
|
for (const size of [1e1, 1e2, 1e3, 1e4, 1e5]) {
|
|
|
|
|
const strings = Array.from({ length: size }, () => "_");
|
|
|
|
|
const expected = strings.join("");
|
|
|
|
|
|
|
|
|
|
await runBenchmark(
|
|
|
|
|
{
|
2025-11-18 17:53:00 +08:00
|
|
|
name: `String concatenation (${size} strings)`,
|
2025-11-04 03:54:35 +08:00
|
|
|
assert: (result) => assert.equal(result, expected),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"Array#join()"() {
|
|
|
|
|
const result = [];
|
2026-06-16 01:33:59 +08:00
|
|
|
// eslint-disable-next-line unicorn/prefer-spread
|
2025-11-04 03:54:35 +08:00
|
|
|
for (const string of strings) {
|
|
|
|
|
result.push(string);
|
|
|
|
|
}
|
|
|
|
|
return result.join("");
|
|
|
|
|
},
|
|
|
|
|
"string +="() {
|
|
|
|
|
let result = "";
|
|
|
|
|
for (const string of strings) {
|
|
|
|
|
result += string;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|