Skip to content

Instantly share code, notes, and snippets.

@blister
Created July 18, 2023 21:00
Show Gist options
  • Save blister/605a6d9f74b9ee6cbfb43c23993833a9 to your computer and use it in GitHub Desktop.
Save blister/605a6d9f74b9ee6cbfb43c23993833a9 to your computer and use it in GitHub Desktop.
JS Loop Speed Tests
const els = 10000000;
console.log('setup -> ' + els);
const opts = { 0: 'Fail!', 1: 'Publish!', 2: 'Publish!' };
let arr = [];
for ( let i = 0; i < els; ++i ) {
if ( Math.floor(Math.random() * 12) > 8 ) {
arr.push('good');
} else {
arr.push('bad');
}
}
const tests = {
'forloop_1': {
'ref': forloop_1,
'average': null,
},
'forloop_2': {
'ref': forloop_2,
'average': null,
},
'forloop_2_1': {
'ref': forloop_2_1,
'average': null,
},
'forloop_3': {
'ref': forloop_3,
'average': null,
},
'foreachloop_1': {
'ref': foreachloop_1,
'average': null,
},
'foreachloop_2': {
'ref': foreachloop_2,
'average': null,
},
'map_1': {
'ref': map_1,
'average': null,
},
'map_2': {
'ref': map_2,
'average': null,
},
'map_3_sorted': {
'ref': map_3_sorted,
'average': null,
},
'forloop_3_sorted': {
'ref': forloop_3_sorted,
'average': null,
},
'while_pop_1': {
'ref': while_pop_1,
'average': null,
},
'while_pop_1_1': {
'ref': while_pop_1_1,
'average': null,
},
'while_shift_1': {
'ref': while_shift_1,
'average': null,
},
};
for ( let test in tests ) {
const t0 = performance.now();
let newArr = Array.from(arr);
let out = tests[test]['ref'](newArr);
const t1 = performance.now();
console.log(`${test} => ${t1 - t0} ms. (${out})`);
tests[ test ]['average'] = t1 - t0;
}
//console.log(tests);
function forloop_1(x) {
let count = 0;
for ( let i = 0; i < x.length; i++ ) {
if ( x[i] === 'good' ) {
count++;
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function forloop_2(x) {
let count = 0;
for ( let i = 0; i < x.length; ++i ) {
if ( x[i] === 'good' ) {
count++;
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
// remove x.lenght check in loop
function forloop_2_1(x) {
let count = 0;
let total = x.length;
for ( let i = 0; i < total; ++i ) {
if ( x[i] === 'good' ) {
count++;
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function forloop_3(x) {
let count = 0;
for ( let i = 0; i < x.length; ++i ) {
if ( x[i] === 'good' ) {
count++;
}
if ( count > 2 ) {
return 'I smell a series!';
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function foreachloop_1(x) {
let count = 0;
for ( let i in x ) {
if ( x[i] === 'good' ) {
count++;
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function foreachloop_2(x) {
let count = 0;
for ( let i in x ) {
if ( x[i] === 'good' ) {
count++;
}
if ( count > 2 ) {
return 'I smell a series!';
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function map_1(x) {
return opts[ x.filter(e => e === 'good').length ] || 'I smell a series!';
}
function map_2(x) {
let num = x.filter(e => e === 'good').length;
if ( num > 2 ) { return 'I smell a series'; }
return opts[ num ];
}
function map_3_sorted(x) {
x.sort();
let num = x.filter(e => e === 'good').length;
if ( num > 2 ) { return 'I smell a series'; }
return opts[ num ];
}
function forloop_3_sorted(x) {
x.sort();
let count = 0;
for ( let i = 0; i < x.length; ++i ) {
if ( x[i] === 'good' ) {
count++;
}
if ( count > 2 ) {
return 'I smell a series!';
}
}
switch(count) {
case 0: return 'Fail!';
case 1:
case 2: return 'Publish!';
default: return 'I smell a series!';
}
}
function while_pop_1(x) {
let count = 0;
let a;
while ( a = x.pop() ) {
if ( a === 'good' ) {
count++;
}
}
if ( count > 2 ) {
return 'I smell a series!';
}
if ( count > 0 ) {
return 'Publish!';
}
return 'Fail!';
}
function while_pop_1_1(x) {
let count = 0;
let a;
while ( a = x.pop() ) {
if ( a === 'good' ) {
count++;
}
if ( count === 3 ) { return 'I smell a series!'; }
}
if ( count > 0 ) {
return 'Publish!';
}
return 'Fail!';
}
function while_shift_1(x) {
return 'skipped';
let count = 0;
let a;
while ( a = x.shift() ) {
if ( a === 'good' ) {
count++;
}
if ( count === 3 ) { return 'I smell a series!'; }
}
if ( count > 0 ) {
return 'Publish!';
}
return 'Fail!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment