Created
September 20, 2023 10:01
-
-
Save amb26/73f51aaf975038c55f8314ba506177af to your computer and use it in GitHub Desktop.
Testing time and space consumption of allocating JSON and DOM nodes - with 0 properties each
This file contains 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
"use strict"; | |
const testDOMSpeed = function (its) { | |
const array = []; | |
for (let i = 0; i < its; ++i) { | |
const element = document.createElement("div"); | |
array.push(element); | |
} | |
return array; | |
}; | |
const testJSONSpeed = function (its) { | |
const array = []; | |
for (let i = 0; i < its; ++i) { | |
const element = {}; | |
array.push(element); | |
} | |
return array; | |
}; | |
const clone = function (toclone) { | |
const togo = {}; | |
for (let key in toclone) { | |
togo[key] = toclone[key]; | |
} | |
return togo; | |
}; | |
const timeIt = function (its, fn, name) { | |
const now = Date.now(); | |
const oldShot = clone(performance.memory); | |
const value = fn(its); | |
const delay = Date.now() - now; | |
const newShot = performance.memory; | |
const bytes = newShot.usedJSHeapSize - oldShot.usedJSHeapSize; | |
console.log("Testing allocation of " + name); | |
console.log(its + " iterations in " + delay + " ms: " + 1000 * (delay / its) + " us/it"); | |
console.log("Used heap size " + bytes + ": " + (bytes / its) + " bytes per object"); | |
console.log("Oldshot" , oldShot); | |
console.log("Newshot", newShot); | |
return value; | |
}; | |
function testSpeed() { | |
timeIt(1000000, testDOMSpeed, "DOM nodes"); | |
timeIt(10000000, testJSONSpeed, "JSON objects"); | |
} | |
document.getElementById("run-tests").addEventListener("click", testSpeed); | |
Testing allocation of DOM nodes | |
testspeed.js:41 1000000 iterations in 698 ms: 0.6980000000000001 us/it | |
testspeed.js:42 Used heap size 34964264: 34.964264 bytes per object | |
testspeed.js:40 Testing allocation of JSON objects | |
testspeed.js:41 10000000 iterations in 2471 ms: 0.2471 us/it | |
testspeed.js:42 Used heap size 342355028: 34.2355028 bytes per object | |
testspeed.js:40 Testing allocation of DOM nodes | |
testspeed.js:41 1000000 iterations in 672 ms: 0.6719999999999999 us/it | |
testspeed.js:42 Used heap size 40833645: 40.833645 bytes per object | |
testspeed.js:40 Testing allocation of JSON objects | |
testspeed.js:41 10000000 iterations in 2512 ms: 0.2512 us/it | |
testspeed.js:42 Used heap size 338121356: 33.8121356 bytes per object | |
testspeed.js:40 Testing allocation of DOM nodes | |
testspeed.js:41 1000000 iterations in 638 ms: 0.638 us/it | |
testspeed.js:42 Used heap size 40987036: 40.987036 bytes per object | |
testspeed.js:40 Testing allocation of JSON objects | |
testspeed.js:41 10000000 iterations in 2010 ms: 0.201 us/it | |
testspeed.js:42 Used heap size 382067844: 38.2067844 bytes per object | |
testspeed.js:40 Testing allocation of DOM nodes | |
testspeed.js:41 1000000 iterations in 702 ms: 0.7020000000000001 us/it | |
testspeed.js:42 Used heap size 36669940: 36.66994 bytes per object | |
testspeed.js:40 Testing allocation of JSON objects | |
testspeed.js:41 10000000 iterations in 2440 ms: 0.244 us/it | |
testspeed.js:42 Used heap size 342350124: 34.2350124 bytes per object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment