intarrayvsbitop

Benchmark created by Sébastien on


Setup

var a = new Int8Array(9);
    var b = 0;
    
    var isTerminal = function(B) {
            if (((B[0] === B[1+3*1]) && (B[0] === B[2 + 3*2])) ||
                ((B[0 + 2*3] === B[1 + 3*1]) && (B[0 + 3*2] === B[2 + 3*0]))) {
                if (B[1 + 3*1] !== 0)
                    return B[1+ 3*1];
            }
            for (var i = 3 - 1; i >= 0; i--) {
                if ((B[i + 3*0] === B[i + 3*1]) && (B[i + 3*0] === B[i + 3*2])) {
                    if (B[i + 3*0] !== 0)
                        return B[i + 3*0];
                }
                if ((B[0 + 3*i] === B[1 + 3*i]) && (B[0 + 3*i] === B[2 + 3*i])) {
                    if (B[0 + 3*i] !== 0)
                        return B[0 + 3*i];
                }
            }
            return 0;
    };
    
        var board2Bin = function(p, r, c) {
          var board = 0;
          for (var i = r.length - 1; i >= 0; i--){
            board = board | p << (r[i] + c[i]*3) * 2;
          };  
          return board;
        };
    
            winningBoards = [
            board2Bin(1, [0, 1, 2], [0, 0, 0]),
            board2Bin(1, [0, 1, 2], [1, 1, 1]),
            board2Bin(1, [0, 1, 2], [2, 2, 2]),
            board2Bin(1, [0, 1, 2], [0, 1, 2]),
            board2Bin(1, [0, 1, 2], [2, 1, 0]),
            board2Bin(1, [0, 0, 0], [0, 1, 2]),
            board2Bin(1, [1, 1, 1], [0, 1, 2]),
            board2Bin(1, [2, 2, 2], [0, 1, 2])        
            ];
    
    var isTerminal2 = function(B){
           return (B & winningBoards[0] === winningBoards[0]) | (B & winningBoards[1] === winningBoards[1]) | (B & winningBoards[2] === winningBoards[2]) | (B & winningBoards[3] === winningBoards[3]) | (B & winningBoards[4] === winningBoards[4]) | (B & winningBoards[5] === winningBoards[5]) | (B & winningBoards[6] === winningBoards[6]) | (B & winningBoards[7] === winningBoards[7]);
    };

Test runner

Ready to run.

Testing in
TestOps/sec
int8array
col = 0;
row = 0;
a[col*3 + row] = 1;
var t = 2*a[col*3 + row];

col = 1;
a[col*3 + row] = 1;
t = 2*a[col*3 + row]

col = 2;
a[col*3 + row] = 1;
t = a[col*3 + row]

//isTerminal(a);
ready
bitop
col = 0;
row = 0;
var index = (col*3 + row)*2
b = b | 1 << index;
var t = b >> index;

col = 1;
index = (col*3 + row)*2
b = b | 1 << index
var t = b >> index;

col = 2;
index = (col*3 + row)*2
b = b | 1 << index;
var t = b >> index;

//isTerminal2(b);
 
ready

Revisions

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

  • Revision 1: published by Sébastien on