Skip to content

Instantly share code, notes, and snippets.

@amans199
Last active October 28, 2021 10:42
Show Gist options
  • Save amans199/bd8bcb77f29e3b04a184ee3cbe9ae178 to your computer and use it in GitHub Desktop.
Save amans199/bd8bcb77f29e3b04a184ee3cbe9ae178 to your computer and use it in GitHub Desktop.
JavaScript Loops Performance test
console.log('Hello World');
//https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript
const timer = function* (time) {
yield new Date().getTime() / 1000 - time
}
const timing = timer(new Date().getTime() / 1000);
let arr = []
const arrLength = 1000000
let bestToWorst = {
'1000_000 reconrd': {
'foreach': '.005999 seconds',
'map': '.00900 seconds ',
'while': '.02200 seconds',
'for': '.038999 seconds',
'map': {
'foreach': '.005 seconds',
'map': '.0099 seconds ',
'while': '.111 seconds',
'for': '.154 seconds',
},
'weakmap': {
'foreach': '.00600 seconds',
'map': '.0099999 seconds',
'while': '.02200 seconds',
'for': '.819 seconds'
},
},
'100 records': {
'while': '.00016 seconds',
'map': '.00016 seconds ',
'foreach': '.00016 seconds',
'for': '.00016 seconds',
'map': {
'map': '.00016 seconds',
'foreach': '.00016 seconds',
'for': '.00016 seconds'
},
'weakmap': {
'map': '.00016 seconds',
'foreach': '.00016 seconds',
'for': '.00016 seconds'
},
},
}
// LOOP WITH WHILE METHOD --------------------->
// while (arr.length < arrLength) {
// arr.push(arrLength)
// }
// console.log('while', timing.next().value);
// .02200 Seconds 1000_000 records
// .00016 Seconds 100 records
// LOOP WITH MAP METHOD --------------------->
// new Array(arrLength).map(element => {
// arr = []
// arr.push(element)
// });
// console.log('map', timing.next().value);
// .00900 seconds 1000_000 records
// .00016 seconds 100 records
// LOOP WITH FOREACH --------------------->
// new Array(arrLength).forEach(element => {
// arr = []
// arr.push(element)
// });
// console.log('foreach', timing.next().value);
// .005999 seconds 1000_000 records
// .00016 seconds 100 records
// LOOP WITH FOR --------------------->
// for (let index = 0; index < arrLength; index++) {
// arr = []
// arr.push(index)
// }
// console.log('for', timing.next().value);
// .038999 seconds 1000_000 records
// .00016 seconds 100 records
// ================== test WeakMap
// const weakMap = new Map();
// LOOP WITH WHILE --------------------->
// while (weakMap.size < arrLength) {
// weakMap.set(i++, arrLength)
// }
// console.log('while', timing.next().value);
// .02200 Seconds 1000_000 records for wakmap
// .111 seconds 100 records for map
// .128 Seconds 100 records for map
// LOOP WITH MAP --------------------->
// new Array(arrLength).map(element => {
// weakMap.set(element, element)
// });
// console.log('map', timing.next().value);
// .0099999 seconds 1000_000 records for weakmap
// .0099 seconds 100 records for map
// .00016 seconds 100 records
// LOOP WITH FOREACH --------------------->
// new Array(arrLength).forEach(element => {
// weakMap.set(element, element)
// });
// console.log('foreach', timing.next().value);
// .00600 seconds 1000_000 records for weakMap
// .005 seconds 1000_000 records for map()
// .00016 seconds 100 records
// LOOP WITH FOR --------------------->
// for (let index = 0; index < arrLength; index++) {
// weakMap.set({ index }, index)
// }
// console.log('for', timing.next().value);
// .819 seconds 1000_000 records weakMap()
// .154 seconds 1000_000 records map()
// .00016 seconds 100 records
// testing advanced for loop
// for (var i = 0, len = arr.length + 1; i < len; i++) {
// for (var i = 0; i < arr.length + 1; i++) {
// if (arr.length === 1000000) break;
// arr[i] = i
// }
// console.log('q', timing.next().value);
// .023 seconds
// .026 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment