binary search versus indexof versus hash key check

JavaScript performance comparison

Test case created by Chris Walker

Preparation code

//Big Array
var props =,
    hash = {};

// random key, there are about 270
var goodkey = props[138], badkey = "I don't exist in the array";

  hash[v] = true

var assert = function(val, expected){
  if(val !== expected){ throw new Error("Bad Assert"); }

var inArrayBinary = function(arr, str) {
  var lo = 0, hi = arr.length, check;
  while (true) {
    check = Math.floor(lo + ((hi - lo) / 2))
    if (arr[check] === str) {
      return true
    if (arr[check] < str) {
      lo = check
    } else {
      hi = check-1
    if (hi === lo) {
      return false

var inArrayNativeIndexOf = function(arr, str){
  return !!~arr.indexOf(str);

var inHash = function(hash, str){
  return !!hash[str];

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
assert(inArrayNativeIndexOf(props, goodkey), true);
assert(inArrayNativeIndexOf(props, badkey), false);
hash key check
assert(inHash(hash, goodkey), true);
assert(inHash(hash, badkey), false);
binary search
assert(inArrayBinary(props, goodkey), true);
assert(inArrayBinary(props, badkey), false);

Compare results of other browsers


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:


Add a comment