Which Method of Iteration in JavaScript is Fastest?
There are many ways to traverse an array in Javascript. In this benchmark, we will look at five different ways and the pros and cons of each. Keep in mind that these benchmarks were run in a Chrome browser on Codepen.io. Results will vary by browser/interpreter.
For a working example of these benchmarks, take a look at this codepen. All benchmarks we ran on an array of 1,000,000,000 items.
1st: Vanilla JS - Backwards
for (let i = arr.length - 1; i >= 0; i--) {}
~ 30 milliseconds
Going backwards is faster than going forward! At each iteration the loop checks against a constant 0 zero value instead of calling the array’s .length property. I highly recommend NOT putting this optimization into practice however, it’s weird and results in hard to understand code.
2nd: Vanilla JS - Forwards
for (let i = 0; i < arr.length; i++) {}
~39 milliseconds
3rd: ES6 forEach()
arr.forEach(function (element) {});
~180 milliseconds
Slow but with a more convenient syntax, nothing surprising here.
4th: jQuery Each
$.each(arr, function (index, value) {});
~225 milliseconds
Eeeeeew… jQuery. Convenient if you live in 2010. Very Slow.
Wildcard: For..Of ES6
for (const item of arr) {
}
First and second time running: 153 Milliseconds
Third+ times running : ~18 milliseconds
This is weird, and I’m not sure how to explain it. Maybe someone smarter than me can tweet me the answer @wagslane . The first two times running this after a fresh browser load are quite slow, but then it gets blazingly fast. I’m assuming there are some es6 optimizations under the hood that kick in.
Related Articles
Singletons in ES6 - The Good, The Bad, The Ugly
Nov 04, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
Singletons are fairly controversial as far as I can tell, especially in JavaScript programming. Let’s take a look at what they are, when to (maybe) use them, and when not to.
How to Recursively Traverse JSON Objects
Sep 22, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
I’ve found that it’s pretty rare that I need recursion in application code, but every once in a while I need to write a function that operates on a tree of unknown depth, such as a JSON object, and that’s often best solved recursively. Even though recursion is rare, it is important to recognize when a problem is best solved recursively so that we can implement a good solution when the time comes.
Secure Random Numbers in Node.js
Jul 03, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
Quick answer: use crypto.randomBytes() for cryptographically secure randomness in Node.js. const { randomBytes } = await import("node:crypto"); const buf = randomBytes(256); console.log(`${buf.length} bytes of random data: ${buf.toString("hex")}`); crypto.randomBytes() is a cryptographically secure random number generator based on openssl. Depending on the operating system of the user, randomBytes will use /dev/urandom (Unix) or CryptoGenRandom (Windows).