1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/examples/array-micro.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,132 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * - Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 1.14 + * - Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 1.18 + * - Neither the name of Oracle nor the names of its 1.19 + * contributors may be used to endorse or promote products derived 1.20 + * from this software without specific prior written permission. 1.21 + * 1.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.25 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + */ 1.34 + 1.35 + 1.36 + 1.37 +function bench(name, func) { 1.38 + var start = Date.now(); 1.39 + for (var iter = 0; iter < 5e6; iter++) { 1.40 + func(); 1.41 + } 1.42 + print((Date.now() - start) + "\t" + name); 1.43 +} 1.44 + 1.45 +bench("[]", function() { 1.46 + []; 1.47 + []; 1.48 + []; 1.49 +}); 1.50 + 1.51 +bench("[1, 2, 3]", function() { 1.52 + [1, 2, 3]; 1.53 + [1, 2, 3]; 1.54 + [1, 2, 3]; 1.55 +}); 1.56 + 1.57 +bench("[1 .. 20]", function() { 1.58 + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; 1.59 + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; 1.60 + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; 1.61 +}); 1.62 + 1.63 +bench("new Array()", function() { 1.64 + new Array(); 1.65 + new Array(); 1.66 + new Array(); 1.67 +}); 1.68 + 1.69 + 1.70 +bench("new Array(1, 2, 3)", function() { 1.71 + new Array(1, 2, 3); 1.72 + new Array(1, 2, 3); 1.73 + new Array(1, 2, 3); 1.74 +}); 1.75 + 1.76 +bench("new Array(10)", function() { 1.77 + new Array(10); 1.78 + new Array(10); 1.79 + new Array(10); 1.80 +}); 1.81 + 1.82 +var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 1.83 + 1.84 +bench("get", function() { 1.85 + array[0]; 1.86 + array[3]; 1.87 + array[6]; 1.88 +}); 1.89 + 1.90 +bench("set", function() { 1.91 + array[0] = 0; 1.92 + array[3] = 3; 1.93 + array[6] = 6; 1.94 +}); 1.95 + 1.96 +bench("push", function() { 1.97 + var arr = [1, 2, 3]; 1.98 + arr.push(4); 1.99 + arr.push(5); 1.100 + arr.push(6); 1.101 +}); 1.102 + 1.103 +bench("pop", function() { 1.104 + var arr = [1, 2, 3]; 1.105 + arr.pop(); 1.106 + arr.pop(); 1.107 + arr.pop(); 1.108 +}); 1.109 + 1.110 +bench("splice", function() { 1.111 + [1, 2, 3].splice(0, 2, 5, 6, 7); 1.112 +}); 1.113 + 1.114 +var all = function(e) { return true; }; 1.115 +var none = function(e) { return false; }; 1.116 + 1.117 +bench("filter all", function() { 1.118 + array.filter(all); 1.119 +}); 1.120 + 1.121 +bench("filter none", function() { 1.122 + array.filter(none); 1.123 +}); 1.124 + 1.125 +var up = function(a, b) { return a > b ? 1 : -1; }; 1.126 +var down = function(a, b) { return a < b ? 1 : -1; }; 1.127 + 1.128 +bench("sort up", function() { 1.129 + [1, 2, 3, 4].sort(up); 1.130 +}); 1.131 + 1.132 +bench("sort down", function() { 1.133 + [1, 2, 3, 4].sort(down); 1.134 +}); 1.135 +