test/examples/array-micro.js

Thu, 17 Oct 2013 17:33:16 +0200

author
hannesw
date
Thu, 17 Oct 2013 17:33:16 +0200
changeset 633
a2065f67857c
parent 334
918a986b0478
child 952
6d5471a497fb
child 962
ac62e33a99b0
permissions
-rw-r--r--

8026701: Array.prototype.splice is slow on dense arrays
Reviewed-by: lagergren, sundar, jlaskey

hannesw@334 1 /*
hannesw@334 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
hannesw@334 3 *
hannesw@334 4 * Redistribution and use in source and binary forms, with or without
hannesw@334 5 * modification, are permitted provided that the following conditions
hannesw@334 6 * are met:
hannesw@334 7 *
hannesw@334 8 * - Redistributions of source code must retain the above copyright
hannesw@334 9 * notice, this list of conditions and the following disclaimer.
hannesw@334 10 *
hannesw@334 11 * - Redistributions in binary form must reproduce the above copyright
hannesw@334 12 * notice, this list of conditions and the following disclaimer in the
hannesw@334 13 * documentation and/or other materials provided with the distribution.
hannesw@334 14 *
hannesw@334 15 * - Neither the name of Oracle nor the names of its
hannesw@334 16 * contributors may be used to endorse or promote products derived
hannesw@334 17 * from this software without specific prior written permission.
hannesw@334 18 *
hannesw@334 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
hannesw@334 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
hannesw@334 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
hannesw@334 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
hannesw@334 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
hannesw@334 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
hannesw@334 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
hannesw@334 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
hannesw@334 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
hannesw@334 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
hannesw@334 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
hannesw@334 30 */
hannesw@334 31
hannesw@334 32
hannesw@334 33
hannesw@334 34 function bench(name, func) {
hannesw@334 35 var start = Date.now();
hannesw@334 36 for (var iter = 0; iter < 5e6; iter++) {
hannesw@334 37 func();
hannesw@334 38 }
hannesw@334 39 print((Date.now() - start) + "\t" + name);
hannesw@334 40 }
hannesw@334 41
hannesw@334 42 bench("[]", function() {
hannesw@334 43 [];
hannesw@334 44 [];
hannesw@334 45 [];
hannesw@334 46 });
hannesw@334 47
hannesw@334 48 bench("[1, 2, 3]", function() {
hannesw@334 49 [1, 2, 3];
hannesw@334 50 [1, 2, 3];
hannesw@334 51 [1, 2, 3];
hannesw@334 52 });
hannesw@334 53
hannesw@334 54 bench("[1 .. 20]", function() {
hannesw@334 55 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
hannesw@334 56 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
hannesw@334 57 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
hannesw@334 58 });
hannesw@334 59
hannesw@334 60 bench("new Array()", function() {
hannesw@334 61 new Array();
hannesw@334 62 new Array();
hannesw@334 63 new Array();
hannesw@334 64 });
hannesw@334 65
hannesw@334 66
hannesw@334 67 bench("new Array(1, 2, 3)", function() {
hannesw@334 68 new Array(1, 2, 3);
hannesw@334 69 new Array(1, 2, 3);
hannesw@334 70 new Array(1, 2, 3);
hannesw@334 71 });
hannesw@334 72
hannesw@334 73 bench("new Array(10)", function() {
hannesw@334 74 new Array(10);
hannesw@334 75 new Array(10);
hannesw@334 76 new Array(10);
hannesw@334 77 });
hannesw@334 78
hannesw@334 79 var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
hannesw@334 80
hannesw@334 81 bench("get", function() {
hannesw@334 82 array[0];
hannesw@334 83 array[3];
hannesw@334 84 array[6];
hannesw@334 85 });
hannesw@334 86
hannesw@334 87 bench("set", function() {
hannesw@334 88 array[0] = 0;
hannesw@334 89 array[3] = 3;
hannesw@334 90 array[6] = 6;
hannesw@334 91 });
hannesw@334 92
hannesw@633 93 bench("push", function() {
hannesw@633 94 var arr = [1, 2, 3];
hannesw@633 95 arr.push(4);
hannesw@633 96 arr.push(5);
hannesw@633 97 arr.push(6);
hannesw@633 98 });
hannesw@633 99
hannesw@633 100 bench("pop", function() {
hannesw@633 101 var arr = [1, 2, 3];
hannesw@633 102 arr.pop();
hannesw@633 103 arr.pop();
hannesw@633 104 arr.pop();
hannesw@633 105 });
hannesw@633 106
hannesw@633 107 bench("splice", function() {
hannesw@633 108 [1, 2, 3].splice(0, 2, 5, 6, 7);
hannesw@633 109 });
hannesw@633 110
hannesw@334 111 var all = function(e) { return true; };
hannesw@334 112 var none = function(e) { return false; };
hannesw@334 113
hannesw@334 114 bench("filter all", function() {
hannesw@334 115 array.filter(all);
hannesw@334 116 });
hannesw@334 117
hannesw@334 118 bench("filter none", function() {
hannesw@334 119 array.filter(none);
hannesw@334 120 });
hannesw@334 121
hannesw@334 122 var up = function(a, b) { return a > b ? 1 : -1; };
hannesw@334 123 var down = function(a, b) { return a < b ? 1 : -1; };
hannesw@334 124
hannesw@334 125 bench("sort up", function() {
hannesw@334 126 [1, 2, 3, 4].sort(up);
hannesw@334 127 });
hannesw@334 128
hannesw@334 129 bench("sort down", function() {
hannesw@334 130 [1, 2, 3, 4].sort(down);
hannesw@334 131 });
hannesw@334 132

mercurial