hannesw@334: /* hannesw@334: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. hannesw@334: * hannesw@334: * Redistribution and use in source and binary forms, with or without hannesw@334: * modification, are permitted provided that the following conditions hannesw@334: * are met: hannesw@334: * hannesw@334: * - Redistributions of source code must retain the above copyright hannesw@334: * notice, this list of conditions and the following disclaimer. hannesw@334: * hannesw@334: * - Redistributions in binary form must reproduce the above copyright hannesw@334: * notice, this list of conditions and the following disclaimer in the hannesw@334: * documentation and/or other materials provided with the distribution. hannesw@334: * hannesw@334: * - Neither the name of Oracle nor the names of its hannesw@334: * contributors may be used to endorse or promote products derived hannesw@334: * from this software without specific prior written permission. hannesw@334: * hannesw@334: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS hannesw@334: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, hannesw@334: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR hannesw@334: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR hannesw@334: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, hannesw@334: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, hannesw@334: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR hannesw@334: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF hannesw@334: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING hannesw@334: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS hannesw@334: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. hannesw@334: */ hannesw@334: hannesw@334: hannesw@334: hannesw@334: function bench(name, func) { hannesw@334: var start = Date.now(); hannesw@334: for (var iter = 0; iter < 5e6; iter++) { hannesw@334: func(); hannesw@334: } hannesw@334: print((Date.now() - start) + "\t" + name); hannesw@334: } hannesw@334: hannesw@334: bench("[]", function() { hannesw@334: []; hannesw@334: []; hannesw@334: []; hannesw@334: }); hannesw@334: hannesw@334: bench("[1, 2, 3]", function() { hannesw@334: [1, 2, 3]; hannesw@334: [1, 2, 3]; hannesw@334: [1, 2, 3]; hannesw@334: }); hannesw@334: hannesw@334: bench("[1 .. 20]", function() { hannesw@334: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; hannesw@334: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; hannesw@334: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; hannesw@334: }); hannesw@334: hannesw@334: bench("new Array()", function() { hannesw@334: new Array(); hannesw@334: new Array(); hannesw@334: new Array(); hannesw@334: }); hannesw@334: hannesw@334: hannesw@334: bench("new Array(1, 2, 3)", function() { hannesw@334: new Array(1, 2, 3); hannesw@334: new Array(1, 2, 3); hannesw@334: new Array(1, 2, 3); hannesw@334: }); hannesw@334: hannesw@334: bench("new Array(10)", function() { hannesw@334: new Array(10); hannesw@334: new Array(10); hannesw@334: new Array(10); hannesw@334: }); hannesw@334: hannesw@334: var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; hannesw@334: hannesw@334: bench("get", function() { hannesw@334: array[0]; hannesw@334: array[3]; hannesw@334: array[6]; hannesw@334: }); hannesw@334: hannesw@334: bench("set", function() { hannesw@334: array[0] = 0; hannesw@334: array[3] = 3; hannesw@334: array[6] = 6; hannesw@334: }); hannesw@334: hannesw@633: bench("push", function() { hannesw@633: var arr = [1, 2, 3]; hannesw@633: arr.push(4); hannesw@633: arr.push(5); hannesw@633: arr.push(6); hannesw@633: }); hannesw@633: hannesw@633: bench("pop", function() { hannesw@633: var arr = [1, 2, 3]; hannesw@633: arr.pop(); hannesw@633: arr.pop(); hannesw@633: arr.pop(); hannesw@633: }); hannesw@633: hannesw@633: bench("splice", function() { hannesw@633: [1, 2, 3].splice(0, 2, 5, 6, 7); hannesw@633: }); hannesw@633: hannesw@334: var all = function(e) { return true; }; hannesw@334: var none = function(e) { return false; }; hannesw@334: hannesw@334: bench("filter all", function() { hannesw@334: array.filter(all); hannesw@334: }); hannesw@334: hannesw@334: bench("filter none", function() { hannesw@334: array.filter(none); hannesw@334: }); hannesw@334: hannesw@334: var up = function(a, b) { return a > b ? 1 : -1; }; hannesw@334: var down = function(a, b) { return a < b ? 1 : -1; }; hannesw@334: hannesw@334: bench("sort up", function() { hannesw@334: [1, 2, 3, 4].sort(up); hannesw@334: }); hannesw@334: hannesw@334: bench("sort down", function() { hannesw@334: [1, 2, 3, 4].sort(down); hannesw@334: }); hannesw@334: