Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/examples/string-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*/303132var str = "The quick gray nashorn jumps over the lazy zebra.";3334function bench(name, func) {35var start = Date.now();36for (var iter = 0; iter < 5000000; iter++) {37func();38}39print((Date.now() - start) + "\t" + name);40}4142bench("[]", function() {43str[0];44str[1];45str[2];46});4748bench("fromCharCode 1", function() {49String.fromCharCode(97);50String.fromCharCode(98);51String.fromCharCode(99);52});5354bench("fromCharCode 2", function() {55String.fromCharCode(97, 98);56String.fromCharCode(97, 98, 99);57String.fromCharCode(97, 98, 99, 100);58});5960bench("charAt 1", function() {61str.charAt(0);62str.charAt(1);63str.charAt(2);64});6566bench("charAt 2", function() {67str.charAt(100);68str.charAt(-1);69});7071bench("charCodeAt 1", function() {72str.charCodeAt(0);73str.charCodeAt(1);74str.charCodeAt(2);75});7677bench("charCodeAt 2", function() {78str.charCodeAt(100);79str.charCodeAt(-1);80});8182bench("indexOf 1", function() {83str.indexOf("T");84str.indexOf("h");85str.indexOf("e");86});8788bench("indexOf 2", function() {89str.indexOf("T", 0);90str.indexOf("h", 1);91str.indexOf("e", 2);92});9394bench("lastIndexOf", function() {95str.indexOf("a");96str.indexOf("r");97str.indexOf("b");98});99100bench("slice", function() {101str.slice(5);102str.slice(5);103str.slice(5);104});105106bench("split 1", function() {107str.split();108});109110bench("split 2", function() {111str.split("foo");112});113114bench("split 3", function() {115str.split(/foo/);116});117118bench("substring 1", function() {119str.substring(0);120str.substring(0);121str.substring(0);122});123124bench("substring 2", function() {125str.substring(0, 5);126str.substring(0, 5);127str.substring(0, 5);128});129130bench("substr", function() {131str.substr(0);132str.substr(0);133str.substr(0);134});135136bench("slice", function() {137str.slice(0);138str.slice(0);139str.slice(0);140});141142bench("concat", function() {143str.concat(str);144str.concat(str);145str.concat(str);146});147148bench("trim", function() {149str.trim();150str.trim();151str.trim();152});153154bench("toUpperCase", function() {155str.toUpperCase();156});157158bench("toLowerCase", function() {159str.toLowerCase();160});161162bench("valueOf", function() {163str.valueOf();164str.valueOf();165str.valueOf();166});167168bench("toString", function() {169str.toString();170str.toString();171str.toString();172});173174bench("String", function() {175String(str);176String(str);177String(str);178});179180bench("new String", function() {181new String(str);182new String(str);183new String(str);184});185186187