Hello, programmers and enthusiastic learner, hope you all having great day. So in last post I had analyze execution time of various loops in javaScript using console object. Today I am going to analyze it using benchmarkjs tool , thanks for Conner Peet for his valuable suggestion.
So let's start...
const cars = ["Tesla", "Tata", "Ford", "Land Rover", "Audi"]; let myCars; //For-let loop bench("for-let i++", () => { for (let i = 0; i < cars.length; i++) { myCars = cars[i]; } }); //For-let (i--) loop bench("for-let i--", () => { for (let i = cars.length - 1; i >= 0; i--) { myCars = cars[i]; } }); //forEach Loop bench("forEach", () => { cars.forEach((car) => { myCars = car; }); }); //For of loop bench("for of", () => { for (const car of cars) { myCars = car; } }); //For in loop bench("for in", () => { for (const car in cars) { myCars = car; } }); //map bench("map", () => cars.map((car) => { myCars = car; }) ); //Output D:\JavaScript\Loops> matcha loops.js 12,200,000 ops/sec > for-let i++ (24.4x) 10,300,000 ops/sec > for-let i-- (20.6x) 11,200,000 ops/sec > forEach (22.4x) 7,080,000 ops/sec > for of (14.1x) 502,000 ops/sec > for in (1x) 5,760,000 ops/sec > map (11.5x) Benches: 6 Fastest: for-let i++ Elapsed: 35.5s
I used this tool, by Conner Peet.
Hope this article add some value to your skill , Your precious suggestions and advice are always welcome!
Top comments (0)