Array contains() implementation
JavaScript performance comparison
Preparation code
<script type="text/javascript">
//<![CDATA[
Array.prototype.contains2perf = function(value) {
var match = false;
this.forEach( function(element) {
(element !== value) || (match = true)
} );
return match;
}
Array.prototype.contains3perf = function(v) {
for(var i = 0, l = this.length; i < l; ++i)
if (this[i] === v) return true;
return false;
}
Array.prototype.contains4perf = function(v) {
return this.indexOf(v) !== -1;
}
var contains1perf_value;
function contains1perf_helper(e) { return e === contains1perf_value; }
Array.prototype.contains1perf = function(v) {
contains1perf_value = v;
return this.some(contains1perf_helper);
}
//]]>
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
some |
|
pending… |
forEach |
|
pending… |
for |
|
pending… |
indexOf |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Kuvam
- Revision 2: published by Tyler Young
0 comments