Skip to content

Instantly share code, notes, and snippets.

@gergelyke
Created April 16, 2017 15:33
Show Gist options
  • Save gergelyke/05c49669563565242d7655fc8adb0e59 to your computer and use it in GitHub Desktop.
Save gergelyke/05c49669563565242d7655fc8adb0e59 to your computer and use it in GitHub Desktop.
Array.pop() vs Array.length=0
var arraySize = 10000
var a = []
var sumTime = 0
function setup () {
for (var i = 0; i < arraySize; i+=1) {
a.push(i)
}
}
function cleanPop () {
while(a.length > 0) {
a.pop()
}
}
function cleanLength () {
a.length = 0
}
for (var i = 0; i < 10000; i+=1) {
setup()
var t1 = process.hrtime()
// cleanPop()
cleanLength()
var diff = process.hrtime(t1)
sumTime += (diff[0] * 1e9 + diff[1])
}
console.log(`took ${sumTime} nanosecs`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment