Testing elements searchs in array.

JavaScript performance comparison

Test case created by Francute

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let trueWasFound = false;
  let arr = [false, false, false, false, false, false, false, false, false, true];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Bad searching with Array.prototype.include()
if (arr.includes(true)) {
 trueWasFound = true;
} else {
 trueWasFound = false;
}
pending…
Good searching with Array.prototype.includes()
trueWasFound = arr.includes(true);
pending…
Loop linear search with keyword for
for (let i = 0, len = arr.length; i < len; i++) {
  if (arr[i] === true) {
    trueWasFound = true;
    break;
  } else {
    trueWasFound = false;
  }
}
pending…
Loop search with Array.prototype.forEach()
arr.forEach(function(element) {
  if (element === true) {
    trueWasFound = true;
  }
});
pending…
Loop search with Array.prototype.forEach() with lambda
arr.forEach((element) => {
  if (element === true) {
    trueWasFound = true;
  }
});
pending…
Loop search with Array.prototype.some()
trueWasFound = arr.some(function(element) {
  return (element === true);
});
pending…
Loop search with Array.prototype.some() with lambda
trueWasFound = arr.some((element) => (element === true));
pending…

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

0 Comments