test/examples/array-micro.js

Wed, 27 Apr 2016 01:36:41 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:36:41 +0800
changeset 0
b1a7da25b547
child 952
6d5471a497fb
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/nashorn/
changeset: 1034:4b9cc65dd24d
tag: jdk8u25-b17

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

mercurial