Last active
November 25, 2025 22:19
-
-
Save yurkimus/1595aed3cc09f15881d28be51b71b68a to your computer and use it in GitHub Desktop.
ECMAScript: Map write optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var N = 1000000; | |
| var RUNS = 100; | |
| const Method = Object.freeze({ 'GET': 'GET' }); | |
| const relation = 'resource'; | |
| const store = []; | |
| const tag = Object.freeze((m, r, p) => m + '/' + r + '/' + p); | |
| const values = new Array(N).fill(0).map((n, i) => ({ id: i + 1, data: 'item' + i })); | |
| console.log(`--- Map Test Start (N=${N}, Runs=${RUNS}) ---`); | |
| function runTest(name, callback) { | |
| let totalTime = 0; | |
| for (let r = 0; r < RUNS; r++) { | |
| const start = performance.now(); | |
| callback(); | |
| const end = performance.now(); | |
| totalTime += (end - start); | |
| } | |
| const avgTime = (totalTime / RUNS).toFixed(2); | |
| console.log(`${name}: ${avgTime} ms (Average)`); | |
| } | |
| runTest("1. C-style, cached tags Array.map", () => { | |
| const tags = Object.freeze(values.map(value => tag(Method['GET'], relation, value.id))); | |
| const l = values.length; | |
| const map1 = new Map(store); | |
| for (let i = 0; i < l; i++) { | |
| map1.set(tags[i], values[i]); | |
| } | |
| }); | |
| runTest("2. C-Style, cached tags Array.from", () => { | |
| const tags = Object.freeze(Array.from(values, value => tag(Method['GET'], relation, value.id))); | |
| const map2 = new Map(store); | |
| const l = values.length; | |
| for (let i = 0; i < l; i++) { | |
| map2.set(tag(Method['GET'], relation, values[i].id), values[i]); | |
| } | |
| }); | |
| runTest("3. C-Style, inlined tag", () => { | |
| const map2 = new Map(store); | |
| const l = values.length; | |
| for (let i = 0; i < l; i++) { | |
| map2.set(tag(Method['GET'], relation, values[i].id), values[i]); | |
| } | |
| }); | |
| runTest("4. C-Style, inlined string concat", () => { | |
| const map2 = new Map(store); | |
| const l = values.length; | |
| for (let i = 0; i < l; i++) { | |
| map2.set(Method['GET'] + '/' + relation + '/' + values[i].id, values[i]); | |
| } | |
| }); | |
| runTest("5. C-Style, inlined template string", () => { | |
| const map2 = new Map(store); | |
| const l = values.length; | |
| for (let i = 0; i < l; i++) { | |
| map2.set(`${Method['GET']}/${relation}/${values[i].id}`, values[i]); | |
| } | |
| }); | |
| runTest("5. C-Style, inlined template string without Method", () => { | |
| const map2 = new Map(store); | |
| const l = values.length; | |
| for (let i = 0; i < l; i++) { | |
| map2.set(`GET/${relation}/${values[i].id}`, values[i]); | |
| } | |
| }); | |
| console.log("--- Map Test End ---"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Map Test Start (N=1000000, Runs=100) --- | |
| 1. C-style, cached tags Array.map: 322.01 ms (Average) | |
| 2. C-Style, cached tags Array.from: 414.11 ms (Average) | |
| 3. C-Style, inlined tag: 277.00 ms (Average) | |
| 4. C-Style, inlined string concat: 275.94 ms (Average) | |
| 5. C-Style, inlined template string: 281.58 ms (Average) | |
| 5. C-Style, inlined template string without Method: 261.91 ms (Average) | |
| --- Map Test End --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Map Test Start (N=1000000, Runs=100) --- | |
| 1. C-style, cached tags Array.map: 313.87 ms (Average) | |
| 2. C-Style, cached tags Array.from: 458.73 ms (Average) | |
| 3. C-Style, inlined tag: 370.30 ms (Average) | |
| 4. C-Style, inlined string concat: 395.40 ms (Average) | |
| 5. C-Style, inlined template string: 413.71 ms (Average) | |
| 5. C-Style, inlined template string without Method: 387.21 ms (Average) | |
| --- Map Test End --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Map Test Start (N=1000000, Runs=100) --- | |
| 1. C-style, cached tags Array.map: 343.22 ms (Average) | |
| 2. C-Style, cached tags Array.from: 511.34 ms (Average) | |
| 3. C-Style, inlined tag: 407.72 ms (Average) | |
| 4. C-Style, inlined string concat: 368.57 ms (Average) | |
| 5. C-Style, inlined template string: 368.17 ms (Average) | |
| 5. C-Style, inlined template string without Method: 353.79 ms (Average) | |
| --- Map Test End --- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment