Skip to content

Instantly share code, notes, and snippets.

@ndabAP
Last active November 4, 2018 07:51
Show Gist options
  • Save ndabAP/59566ecf30b48981a2176ea50aafb151 to your computer and use it in GitHub Desktop.
Save ndabAP/59566ecf30b48981a2176ea50aafb151 to your computer and use it in GitHub Desktop.
Untyped vs. typed JavaScript Arrays
const { performance } = require('perf_hooks')
const start = performance.now()
const buffer = new ArrayBuffer(393216)
let uInt32View = new Uint32Array(buffer)
const length = uInt32View.length
for (let i = 0; i < length; i++) {
uInt32View[i] = i
}
const end = performance.now()
console.log(uInt32View.length)
console.log(uInt32View[uInt32View.length - 1])
console.info(end - start) // ~1.569806999526918
const { performance } = require('perf_hooks')
const start = performance.now()
let untypedArray = []
for (let i = 0; i < 98304; i++) {
untypedArray[i] = i
}
const end = performance.now()
console.log(untypedArray.length)
console.log(untypedArray[untypedArray.length - 1])
console.info(end - start) // ~2.792820000089705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment