Skip to content

Instantly share code, notes, and snippets.

@mxmaxime
Last active September 28, 2017 13:08
Show Gist options
  • Save mxmaxime/5e6a08d00264773bfaf0681c06b6c864 to your computer and use it in GitHub Desktop.
Save mxmaxime/5e6a08d00264773bfaf0681c06b6c864 to your computer and use it in GitHub Desktop.
array includes #jsbench #jsperf (https://jsbench.github.io/#5e6a08d00264773bfaf0681c06b6c864) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>array includes #jsbench #jsperf</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;
suite.add("https://tc39.github.io/ecma262/#sec-array.prototype.includes", function () {
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
// NOTE: === provides the correct "SameValueZero" comparison needed here.
if (o[k] === searchElement) {
return true;
}
k++;
}
// 8. Return false
return false;
}
});
}
var s = ["vsly.co", "viously.com", "kol.to", "fdpdu.net", "viously.fr", "viously.net", "viously.to", "viously.eu", "vs.ly", "vroumvroum.fr", "vious.ly", "vvvv.fr", "vvvv.com", "ffffff.fr", "fffff.com", "fdffff.eu", "mmmmm.fr", "rrrrrr.fr", "rrrrr.com", "zzzzz.fr"];
if(s.includes("rrrrr.com")) {
console.log('includes');
}
});
suite.add("var s = [\"vsly.co\", \"viously.com\", \"kol.to\", \"fdpdu.net\", \"viously.fr\", \"viously.net\", \"viously.to\", \"viously.eu\", \"vs.ly\", \"vroumvroum.fr\", \"vious.ly\", \"vvvv.fr\", \"vvvv.com\", \"ffffff.fr\", \"fffff.com\", \"fdffff.eu\", \"mmmmm.fr\", \"rrrrrr.fr\", \"rrrrr.com\", \"zzzzz.fr\"];", function () {
var s = ["vsly.co", "viously.com", "kol.to", "fdpdu.net", "viously.fr", "viously.net", "viously.to", "viously.eu", "vs.ly", "vroumvroum.fr", "vious.ly", "vvvv.fr", "vvvv.com", "ffffff.fr", "fffff.com", "fdffff.eu", "mmmmm.fr", "rrrrrr.fr", "rrrrr.com", "zzzzz.fr"];
for(var i = 0; i < s.length; i++) {
if (s[i] == "rrrrr.com") {
console.log('includes');
}
}
});
suite.add("var s = [\"vsly.co\", \"viously.com\", \"kol.to\", \"fdpdu.net\", \"viously.fr\", \"viously.net\", \"viously.to\", \"viously.eu\", \"vs.ly\", \"vroumvroum.fr\", \"vious.ly\", \"vvvv.fr\", \"vvvv.com\", \"ffffff.fr\", \"fffff.com\", \"fdffff.eu\", \"mmmmm.fr\", \"rrrrrr.fr\", \"rrrrr.com\", \"zzzzz.fr\"];", function () {
var s = ["vsly.co", "viously.com", "kol.to", "fdpdu.net", "viously.fr", "viously.net", "viously.to", "viously.eu", "vs.ly", "vroumvroum.fr", "vious.ly", "vvvv.fr", "vvvv.com", "ffffff.fr", "fffff.com", "fdffff.eu", "mmmmm.fr", "rrrrrr.fr", "rrrrr.com", "zzzzz.fr"];
if(!s.indexOf(19) > -1) {
console.log('includes');
}
});
suite.add("var s = [\"vsly.co\", \"viously.com\", \"kol.to\", \"fdpdu.net\", \"viously.fr\", \"viously.net\", \"viously.to\", \"viously.eu\", \"vs.ly\", \"vroumvroum.fr\", \"vious.ly\", \"vvvv.fr\", \"vvvv.com\", \"ffffff.fr\", \"fffff.com\", \"fdffff.eu\", \"mmmmm.fr\", \"rrrrrr.fr\", \"rrrrr.com\", \"zzzzz.fr\"];", function () {
var s = ["vsly.co", "viously.com", "kol.to", "fdpdu.net", "viously.fr", "viously.net", "viously.to", "viously.eu", "vs.ly", "vroumvroum.fr", "vious.ly", "vvvv.fr", "vvvv.com", "ffffff.fr", "fffff.com", "fdffff.eu", "mmmmm.fr", "rrrrrr.fr", "rrrrr.com", "zzzzz.fr"];
var contains = function(subject, what) {
var i = subject.length;
while (i--) {
if (subject[i] == what) {
return true;
}
}
return false;
};
if(contains(s, "rrrrr.com")) {
console.log('includes');
}
});
suite.add("", function () {
});
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("array includes #jsbench #jsperf");
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