mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-27 06:43:19 +08:00
A method call on a value of unknown type (untyped JavaScript, `any`) picked its builtin by comparing the method name with strcmp against each candidate, in long ladders per receiver kind: a string method could test dozens of names before it matched. - The name is now looked up once per call in a generated hash table (scr_dyn_methods.h, from scripts/gen-dyn-methods.mjs), and each ladder compares the resulting id. The table verifies the name's bytes, so a name is matched by its content, as before. - The worker event methods compare the first character before strcmp. - bench/dynamic-methods calls string, array, Map, Set, number and own methods from untyped JavaScript.
37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
// Untyped JavaScript calling builtin methods on values it never declares
|
|
// types for: string slice/indexOf/toUpperCase/startsWith/padStart, array
|
|
// push/pop/at/join/includes/indexOf, Map and Set get/set/has/add, number
|
|
// toFixed, and calls of an object's own methods. Argument: the number of
|
|
// iterations.
|
|
const rounds = Number(process.argv[2] ?? "300000");
|
|
const words = ["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta"];
|
|
const counts = new Map();
|
|
const seen = new Set();
|
|
const stack = [];
|
|
const recent = [];
|
|
const tally = {
|
|
total: 0,
|
|
add(value) {
|
|
this.total += value;
|
|
return this.total;
|
|
},
|
|
};
|
|
let checksum = 0;
|
|
for (let i = 0; i < rounds; i++) {
|
|
const word = words[i % words.length];
|
|
const head = word.slice(0, 3).toUpperCase();
|
|
const key = head.padStart(4, "-");
|
|
if (word.indexOf("e") >= 0 && !word.startsWith("z")) checksum += 1;
|
|
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
seen.add(i % 97);
|
|
if (seen.has(i % 89)) checksum += 2;
|
|
stack.push(i);
|
|
if (stack.length > 32) checksum += stack.pop() % 3;
|
|
recent.push(word);
|
|
if (recent.length > 4) recent.shift();
|
|
if (recent.includes("gamma")) checksum += recent.indexOf("gamma");
|
|
checksum += tally.add(i % 5) % 11;
|
|
if (i % 1000 === 0) checksum += Number((i / 7).toFixed(2)) | 0;
|
|
}
|
|
console.log(checksum, counts.size, seen.size, stack.at(-1), recent.join(","), tally.total);
|