Checking if key exists in a JavaScript Array? (v16)

Revision 16 of this benchmark created by Jamie Pate on


Preparation HTML

<script>
var short_text = "That is the Impression that I get";

var words = short_text.split(/\s+/);
array = [], obj = {};
var i , length = words.length; 
for (i =0; i <  length; i++) {
    array[words[i]]= (array[words[i]] || 0) + 1;
    obj[words[i]] = array[words[i]];
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using in
var result = "Impression" in array;
ready
Has the property
var result = array.hasOwnProperty("Impression");
ready
Array as Map
var result = array["Impression"];
ready
Array as Map !== undefined
var result = array["Impression"] !== undefined;
ready
Obj is
var result = "Impression" in obj;
ready
Obj prop
var result = obj["Impression"] !== undefined;
ready

Revisions

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