use performance.now() instead of Date.now() in benchmark

This commit is contained in:
Joeri Exelmans 2025-05-10 11:13:47 +02:00
parent a839d69c0e
commit 9c79d13b1f

View file

@ -5,24 +5,24 @@ function benchmark(N) {
// in-place update of native Map: fastest // in-place update of native Map: fastest
function map() { function map() {
let t = new Map(); let t = new Map();
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
t.set(Math.random(), Math.random()); t.set(Math.random(), Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durInPlace = (N <= 5000000) ? map() : "(skip)"; const durInPlace = (N <= 5000000) ? map() : "";
console.log("in-place:", durInPlace, "ms"); console.log("in-place:", durInPlace, "ms");
// purely functional red-black tree: slower by constant factor // purely functional red-black tree: slower by constant factor
function fun() { function fun() {
let t = createTree(); let t = createTree();
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
t = t.insert(Math.random(), Math.random()); t = t.insert(Math.random(), Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durFunc = fun(); const durFunc = fun();
@ -32,27 +32,27 @@ function benchmark(N) {
function funNoGC() { function funNoGC() {
const trees = new Array(N); const trees = new Array(N);
let t = createTree(); let t = createTree();
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
trees[i] = t; // prevent garbage collection trees[i] = t; // prevent garbage collection
t = t.insert(Math.random(), Math.random()); t = t.insert(Math.random(), Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durFuncNoGC = (N <= 1000000) ? funNoGC() : "(skip)"; const durFuncNoGC = (N <= 1000000) ? funNoGC() : "";
console.log("functional red-black (no GC):", durFuncNoGC, "ms"); console.log("functional red-black (no GC):", durFuncNoGC, "ms");
// purely functional radix tree // purely functional radix tree
function trie() { function trie() {
let t = emptyTrie; let t = emptyTrie;
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
// of course we can only insert strings into radix tree: // of course we can only insert strings into radix tree:
t = insert(t)(String(Math.random()*10))(Math.random()); t = insert(t)(String(Math.random()*10))(Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durTrie = trie(); const durTrie = trie();
@ -62,52 +62,52 @@ function benchmark(N) {
function trieNoGC() { function trieNoGC() {
const trees = new Array(N); const trees = new Array(N);
let t = emptyTrie; let t = emptyTrie;
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
trees[i] = t; // prevent garbage collection trees[i] = t; // prevent garbage collection
// of course we can only insert strings into radix tree: // of course we can only insert strings into radix tree:
t = insert(t)(String(Math.random()*10))(Math.random()); t = insert(t)(String(Math.random()*10))(Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durTrieNoGC = (N <= 1000000) ? trieNoGC() : "(skip)"; const durTrieNoGC = (N <= 1000000) ? trieNoGC() : "";
console.log("functional radix trie (no GC):", durTrieNoGC, "ms"); console.log("functional radix trie (no GC):", durTrieNoGC, "ms");
// defensive copy of native Map: slowest (won't scale) // defensive copy of native Map: slowest (won't scale)
function copying() { function copying() {
let t = new Map(); let t = new Map();
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
t = new Map(t); t = new Map(t);
t.set(Math.random(), Math.random()); t.set(Math.random(), Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durCopying = (N <= 10000) ? copying() : "(skip)"; const durCopying = (N <= 10000) ? copying() : "";
console.log("copying:", durCopying, "ms"); console.log("copying:", durCopying, "ms");
// defensive copy of native Map (no garbage collection): slower than slowest // defensive copy of native Map (no garbage collection): slower than slowest
function copyingNoGC() { function copyingNoGC() {
const maps = new Array(N); const maps = new Array(N);
let t = new Map(); let t = new Map();
const startTime = Date.now(); const startTime = performance.now();
for (let i=0; i<N; ++i) { for (let i=0; i<N; ++i) {
maps[i] = t; // prevent garbage collection maps[i] = t; // prevent garbage collection
t = new Map(t); t = new Map(t);
t.set(Math.random(), Math.random()); t.set(Math.random(), Math.random());
} }
const endTime = Date.now(); const endTime = performance.now();
return endTime - startTime; return endTime - startTime;
} }
const durCopyingNoGC = (N <= 10000) ? copyingNoGC() : "(skip)"; const durCopyingNoGC = (N <= 10000) ? copyingNoGC() : "";
console.log("copying (no GC):", durCopyingNoGC, "ms"); console.log("copying (no GC):", durCopyingNoGC, "ms");
return [N, durInPlace, durFunc, durFuncNoGC, durTrie, durTrieNoGC, durCopying, durCopyingNoGC]; return [N, durInPlace, durFunc, durFuncNoGC, durTrie, durTrieNoGC, durCopying, durCopyingNoGC];
} }
const startTime = Date.now(); const startTime = performance.now();
const results = []; const results = [];
const datapoints = [100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000]; const datapoints = [100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000];
@ -118,11 +118,15 @@ for (let i=0; i<datapoints.length; i++) {
results.push(benchmark(N)); results.push(benchmark(N));
} }
const endTime = Date.now(); const endTime = performance.now();
console.log("Benchmark took", endTime-startTime, "ms"); console.log("Benchmark took", endTime-startTime, "ms");
console.log();
console.log("N;in-place;pure-redblack;pure-redblack-NoGC;pure-radix;pure-radix-NoGC;copying;copying-NoGC"); console.log("N;in-place;pure-redblack;pure-redblack-NoGC;pure-radix;pure-radix-NoGC;copying;copying-NoGC");
for (const [N, ...rest] of results) { for (const [N, ...rest] of results) {
console.log(`${N};${rest.join(';')}`); console.log(`${N};${rest.join(';')}`);
} }
console.log();
console.log("now go and paste the above CSV in this spreadsheet:");
console.log(" https://docs.google.com/spreadsheets/d/1WAl5yCZ0UA0XCBn4xphz4foynQpXih4e59kvkbUANRA/edit?usp=sharing");