Skip to content

Instantly share code, notes, and snippets.

@AviKav
Forked from TimothyGu/index.html
Last active March 30, 2019 02:11
Show Gist options
  • Save AviKav/3e02cc5ae174106bb0de5e00435a3b7e to your computer and use it in GitHub Desktop.
Save AviKav/3e02cc5ae174106bb0de5e00435a3b7e to your computer and use it in GitHub Desktop.
for-of loop vs forEach (https://jsbench.github.io/#3e02cc5ae174106bb0de5e00435a3b7e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>for-of loop vs forEach</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
var i,
value,
length,
values = [],
sum = 0,
context = values;
for (i = 0; i < 10000; i++) {
values[i] = Math.random();
}
function add(val) {
sum += val;
}
values.referenceImplementation = (function(callback/*, thisArg*/) {
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = arguments[1];
}
// 6. Let k be 0.
k = 0;
// 7. Repeat while k < len.
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator.
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c.
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined.
})
Array.prototype.forLoopSimplePrototype = (function() {
for (i = 0; i < this.length; i++) {
}
})
values.modifiedReferenceImplementation = (function(callback) {
var k = 0;
while (k < values.length) {
callback(values[k]);
k++;
}
})
};
suite.add("values.forEach(()=>{})", function () {
//values.forEach(()=>{})
=
});
suite.add("for (i = 0; i < values.length; i++){}", function () {
for (i = 0; i < values.length; i++){}
});
suite.add("for (value of values){}", function () {
//for (value of values){}
=
});
suite.add("values.referenceImplementation(()=>{})", function () {
values.referenceImplementation(()=>{})
});
suite.add("values.forLoopSimplePrototype()", function () {
//values.forLoopSimplePrototype()
=
});
suite.add("var k = 0;", function () {
var k = 0;
while (k < values.length) {
(()=>{})(values[k]);
k++;
}
});
suite.add("var k = 0;", function () {
var k = 0;
var length = values.length
while (k < length) {
(()=>{})(values[k]);
k++;
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("for-of loop vs forEach");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment