for vs forEach vs while vs Underscore.js (v391)

Revision 391 of this benchmark created on


Description

Is it faster to use the native forEach or just loop with for?

Preparation HTML

<script src="https://raw.githubusercontent.com/lodash/lodash/3.10.1/lodash.min.js"></script>
<script>
  const values = new Set()
  for (var i = 0; i < 10000; i++) {
    values.add(i)
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
var a = 0
values.forEach(function(i){
  a += i
})
ready
for...of
var a = 0
for (var i of values) {
 a += i
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.