Created
April 16, 2017 15:33
-
-
Save gergelyke/05c49669563565242d7655fc8adb0e59 to your computer and use it in GitHub Desktop.
Array.pop() vs Array.length=0
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 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