luamin: indexOf vs. has

JavaScript performance comparison

Test case created by Mathias Bynens

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
        var indexOf = function(array, value) {
                var index = -1;
                var length = array.length;
                while (++index < length) {
                        if (array[index] == value) {
                                return index;
                        }
                }
        };
   
    var hasOwnProperty = {}.hasOwnProperty;
   
    var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_'];
    var object = { 'a': true, 'b': true, 'c': true, 'd': true, 'e': true, 'f': true, 'g': true, 'h': true, 'i': true, 'j': true, 'k': true, 'l': true, 'm': true, 'n': true, 'o': true, 'p': true, 'q': true, 'r': true, 's': true, 't': true, 'u': true, 'v': true, 'w': true, 'x': true, 'y': true, 'z': true, 'A': true, 'B': true, 'C': true, 'D': true, 'E': true, 'F': true, 'G': true, 'H': true, 'I': true, 'J': true, 'K': true, 'L': true, 'M': true, 'N': true, 'O': true, 'P': true, 'Q': true, 'R': true, 'S': true, 'T': true, 'U': true, 'V': true, 'W': true, 'X': true, 'Y': true, 'Z': true, '_': true };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
indexOf
indexOf(array, 'a');
indexOf(array, 'm');
indexOf(array, 'M');
indexOf(array, 'Z');
indexOf(array, '_');
pending…
has
hasOwnProperty.call(object, 'a');
hasOwnProperty.call(object, 'm');
hasOwnProperty.call(object, 'M');
hasOwnProperty.call(object, 'Z');
hasOwnProperty.call(object, '_');
pending…

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

Compare results of other browsers

0 comments

Add a comment