Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/examples/array-micro.js
32285 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/30313233function bench(name, func) {34var start = Date.now();35for (var iter = 0; iter < 5e6; iter++) {36func();37}38print((Date.now() - start) + "\t" + name);39}4041bench("[]", function() {42[];43[];44[];45});4647bench("[1, 2, 3]", function() {48[1, 2, 3];49[1, 2, 3];50[1, 2, 3];51});5253bench("[1 .. 20]", function() {54[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];55[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];56[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];57});5859bench("new Array()", function() {60new Array();61new Array();62new Array();63});646566bench("new Array(1, 2, 3)", function() {67new Array(1, 2, 3);68new Array(1, 2, 3);69new Array(1, 2, 3);70});7172bench("new Array(10)", function() {73new Array(10);74new Array(10);75new Array(10);76});7778var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];7980bench("get", function() {81array[0];82array[3];83array[6];84});8586bench("set", function() {87array[0] = 0;88array[3] = 3;89array[6] = 6;90});9192bench("push", function() {93var arr = [1, 2, 3];94arr.push(4);95arr.push(5);96arr.push(6);97});9899bench("pop", function() {100var arr = [1, 2, 3];101arr.pop();102arr.pop();103arr.pop();104});105106bench("splice", function() {107[1, 2, 3].splice(0, 2, 5, 6, 7);108});109110var all = function(e) { return true; };111var none = function(e) { return false; };112113bench("filter all", function() {114array.filter(all);115});116117bench("filter none", function() {118array.filter(none);119});120121var up = function(a, b) { return a > b ? 1 : -1; };122var down = function(a, b) { return a < b ? 1 : -1; };123124bench("sort up", function() {125[1, 2, 3, 4].sort(up);126});127128bench("sort down", function() {129[1, 2, 3, 4].sort(down);130});131132133134