binary or vs string concat

Benchmark created on


Setup

var e1 = {metaKey:1,shiftKey:1,altKey:1,ctrlKey:1}
    var e2 = {metaKey:1,shiftKey:1,altKey:1,ctrlKey:0}
    var e3 = {metaKey:0,shiftKey:1,altKey:1,ctrlKey:0}
    var e4 = {metaKey:0,shiftKey:0,altKey:1,ctrlKey:0}
    g1 = function(e){
        return 0 | (e.metaKey ? 1 : 0) | (e.altKey ? 2 : 0)
                | (e.shiftKey ? 4 : 0) | (e.ctrlKey ? 8 : 0);
    }
    g2 = function(e){
        return (e.metaKey ? "1" : "") + (e.altKey ? "2" : "")
                + (e.shiftKey ? "4" : "") + (e.ctrlKey ? "8" : "");            
    }
    g3 = function(e){
        var a = ""
        if(e.metaKey) a+= "1"
        if(e.altKey) a+= "2"
        if(e.shiftKey) a+= "4"
        if(e.ctrlKey) a+= "8"    
        return a
    }

Test runner

Ready to run.

Testing in
TestOps/sec
1
[g1(e1),g1(e2),g1(e3),g1(e4)]
ready
2
[g2(e1),g2(e2),g2(e3),g2(e4)]
ready
3
[g3(e1),g3(e2),g3(e3),g3(e4)]
ready

Revisions

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