Skip to main content
new tests
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360

Esoteric mutable way

Esoteric

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2022-11-13) I perform a test on Chrome 107, Safari 15.2 and Firefox 106 on chosen solutions.

Conclusions

  • solutions C and D are fast or fastest on all browsers for all arrays.
  • solution A and B are slowest on all browsers for all arrays

Results

enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

The below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

enter image description here

Esoteric mutable way

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Esoteric

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2022-11-13) I perform a test on Chrome 107, Safari 15.2 and Firefox 106 on chosen solutions.

Conclusions

  • solutions C and D are fast or fastest on all browsers for all arrays.
  • solution A and B are slowest on all browsers for all arrays

Results

enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

The below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

enter image description here

Rollback to Revision 1
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360

Esoteric, but fast

Esoteric mutable way

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2020-08-05) I perform a test on Chrome 84, Safari 13 and Firefox 78 on chosen solutions.

Conclusions

  • while solution (A) is the fastest on all browsers for all arrays except the big one on Chrome (which is surprising).
  • for medium arrays the while solution (A) is thousands of times faster than other solutions (!!!)
  • The while solution (A) slows down on Chrome for arrays witch 30-40k elements
  • The for-in solution (B) is slowest

Results

Enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

The below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

Enter image description here

Esoteric, but fast

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2020-08-05) I perform a test on Chrome 84, Safari 13 and Firefox 78 on chosen solutions.

Conclusions

  • while solution (A) is the fastest on all browsers for all arrays except the big one on Chrome (which is surprising).
  • for medium arrays the while solution (A) is thousands of times faster than other solutions (!!!)
  • The while solution (A) slows down on Chrome for arrays witch 30-40k elements
  • The for-in solution (B) is slowest

Results

Enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

The below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

Enter image description here

Esoteric mutable way

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> <https://www.youtube.com/watch?v=1Dax90QyXgI&t=19m05s>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

Esoteric, but fast

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2020-08-05) I perform a test on Chrome 84, Safari 13 and Firefox 78 on chosen solutions.

Conclusions

  • while solution (A) is the fastest on all browserbrowsers for all arrays except the big one on Chrome (which is surprising).
  • for medium arrays the while solution (A) is thousands of times faster than other solutions (!!!)
  • The while solution (A) slowsslows down on chromeChrome for arrays witch 30-40k elements
  • The for-in solution (B) is slowest

Results

enter image description hereEnter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

BelowThe below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

enter image description hereEnter image description here

Esoteric but fast

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2020-08-05) I perform test on Chrome 84, Safari 13 and Firefox 78 on chosen solutions.

Conclusions

  • while solution (A) is fastest on all browser for all arrays except big one on Chrome (which is surprising).
  • for medium arrays while solution (A) is thousands of times faster than other solutions (!!!)
  • while solution (A) slows down on chrome for arrays witch 30-40k elements
  • for-in solution (B) is slowest

Results

enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

Below snippet presents code used in test

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for medium array

enter image description here

Esoteric, but fast

let a= ["Hello", "World"];

while(a.length) { console.log( a.shift() ); }

Performance test

Today (2020-08-05) I perform a test on Chrome 84, Safari 13 and Firefox 78 on chosen solutions.

Conclusions

  • while solution (A) is the fastest on all browsers for all arrays except the big one on Chrome (which is surprising).
  • for medium arrays the while solution (A) is thousands of times faster than other solutions (!!!)
  • The while solution (A) slows down on Chrome for arrays witch 30-40k elements
  • The for-in solution (B) is slowest

Results

Enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run it here
  • medium - for 10K elements array and - you can run it here
  • big - for 100K elements array - you can run it here

The below snippet presents code used in the test.

function A(a) {
  let r=0;
  while(a.length) r+= a.shift().length;
  return r;
}

function B(a) {
  let r=0;
  for(i in a) r+= a[i].length;
  return r;
}

function C(a) {
  let r=0;
  for(x of a) r+= x.length;
  return r;
}

function D(a) {
  let r=0;
  for (i=0; i<a.length; ++i) r+= a[i].length;
  return r;

}

function E(a) {
  let r=0;
  a.forEach(x=> r+= x.length);
  return r;
}

let arr= ["Hello", "World!"];
[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

Enter image description here

deleted 10 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
edited body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 351 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 180 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
deleted 2 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 2 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
deleted 5 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
deleted 5 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 2 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
deleted 4 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 231 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
added 231 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading