test expand for loop

JavaScript performance comparison

Test case created by ukyo

Preparation code

<script>
var types = {
    'ui16le': ['Uint16', 2, true],
    'ui16be': ['Uint16', 2, false],
    'ui32le': ['Uint32', 4, true],
    'ui32be': ['Uint32', 4, false],
    'i16le': ['Int16', 2, true],
    'i16be': ['Int16', 2, false],
    'i32le': ['Int32', 4, true],
    'i32be': ['Int32', 4, false],
    'f32le': ['Float32', 4, true],
    'f32be': ['Float32', 4, false],
    'f64le': ['Float64', 8, true],
    'f64be': ['Float64', 8, false]
};

var createBuffReader = function(type, n) {
    var body = [];
    var get = 'arr[++i] = view.get' + type[0] + '(offset += ' + type[1] + ', ' + type[2] + ');';
    var gets = [];
    
    body.push('var view = new DataView(buff, byteOffset, byteLength);');
    body.push('var arr = new ' + type[0] + 'Array(byteLength / ' + type[1] + ');');
    body.push('var i = -1, offset = -' + type[1] + ', len = byteLength + offset;');
    body.push('while(offset < len) {');
    
    for(var i = 0, j = 1; i < n; ++i, j <<= 1) {
        var s = 'if(len - offset >= '+ (j * type[1]) + ') {';
        s += get;
        s += 'continue;}';
        get += get;
        gets[i] = s;
    }
    
    gets.reverse();
    body.push(gets.join('\n'));
    body.push('}return arr;');
    
    return new Function('buff', 'byteOffset', 'byteLength', body.join('\n'));
};

readf64_expand_1 = createBuffReader(types.f64le, 5);
readf64_expand_2 = createBuffReader(types.f64le, 10);
readf64_normal = function(buff, byteOffset, byteLength) {
    var view = new DataView(buff, byteOffset, byteLength);
    var arr = new Float64Array(byteLength / 8);
    for(var i = 0, offset = 0; offset < byteLength; ++i, offset += 8) {
        arr[i] = view.getFloat64(offset, true);
    }
    return arr;
};

f64arr = new Float64Array(80000);
for(var i = 0; i < 80000; ++i) f64arr[i] = Math.random();
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
normal
readf64_normal(f64arr.buffer, 0, f64arr.byteLength);
pending…
expand 1
readf64_expand_1(f64arr.buffer, 0, f64arr.byteLength);
pending…
expand 2
readf64_expand_2(f64arr.buffer, 0, f64arr.byteLength);
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