test/examples/string-micro.js

changeset 0
b1a7da25b547
child 962
ac62e33a99b0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/examples/string-micro.js	Wed Apr 27 01:36:41 2016 +0800
     1.3 @@ -0,0 +1,179 @@
     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 +var str = "The quick gray nashorn jumps over the lazy zebra.";
    1.37 +
    1.38 +function bench(name, func) {
    1.39 +    var start = Date.now();
    1.40 +    for (var iter = 0; iter < 5000000; iter++) {
    1.41 +        func();
    1.42 +    }
    1.43 +    print((Date.now() - start) + "\t" + name);
    1.44 +}
    1.45 +
    1.46 +bench("[]", function() {
    1.47 +    str[0];
    1.48 +    str[1];
    1.49 +    str[2];
    1.50 +});
    1.51 +
    1.52 +bench("fromCharCode", function() {
    1.53 +    String.fromCharCode(97);
    1.54 +    String.fromCharCode(98);
    1.55 +    String.fromCharCode(99);
    1.56 +});
    1.57 +
    1.58 +bench("charAt 1", function() {
    1.59 +    str.charAt(0);
    1.60 +    str.charAt(1);
    1.61 +    str.charAt(2);
    1.62 +});
    1.63 +
    1.64 +bench("charAt 2", function() {
    1.65 +    str.charAt(100);
    1.66 +    str.charAt(-1);
    1.67 +});
    1.68 +
    1.69 +bench("charCodeAt 1", function() {
    1.70 +    str.charCodeAt(0);
    1.71 +    str.charCodeAt(1);
    1.72 +    str.charCodeAt(2);
    1.73 +});
    1.74 +
    1.75 +bench("charCodeAt 2", function() {
    1.76 +    str.charCodeAt(100);
    1.77 +    str.charCodeAt(-1);
    1.78 +});
    1.79 +
    1.80 +bench("indexOf 1", function() {
    1.81 +    str.indexOf("T");
    1.82 +    str.indexOf("h");
    1.83 +    str.indexOf("e");
    1.84 +});
    1.85 +
    1.86 +bench("indexOf 2", function() {
    1.87 +    str.indexOf("T", 0);
    1.88 +    str.indexOf("h", 1);
    1.89 +    str.indexOf("e", 2);
    1.90 +});
    1.91 +
    1.92 +bench("lastIndexOf", function() {
    1.93 +    str.indexOf("a");
    1.94 +    str.indexOf("r");
    1.95 +    str.indexOf("b");
    1.96 +});
    1.97 +
    1.98 +bench("slice", function() {
    1.99 +    str.slice(5);
   1.100 +    str.slice(5);
   1.101 +    str.slice(5);
   1.102 +});
   1.103 +
   1.104 +bench("split 1", function() {
   1.105 +    str.split();
   1.106 +});
   1.107 +
   1.108 +bench("split 2", function() {
   1.109 +    str.split("foo");
   1.110 +});
   1.111 +
   1.112 +bench("split 3", function() {
   1.113 +    str.split(/foo/);
   1.114 +});
   1.115 +
   1.116 +bench("substring 1", function() {
   1.117 +    str.substring(0);
   1.118 +    str.substring(0);
   1.119 +    str.substring(0);
   1.120 +});
   1.121 +
   1.122 +bench("substring 2", function() {
   1.123 +    str.substring(0, 5);
   1.124 +    str.substring(0, 5);
   1.125 +    str.substring(0, 5);
   1.126 +});
   1.127 +
   1.128 +bench("substr", function() {
   1.129 +    str.substr(0);
   1.130 +    str.substr(0);
   1.131 +    str.substr(0);
   1.132 +});
   1.133 +
   1.134 +bench("slice", function() {
   1.135 +    str.slice(0);
   1.136 +    str.slice(0);
   1.137 +    str.slice(0);
   1.138 +});
   1.139 +
   1.140 +bench("concat", function() {
   1.141 +    str.concat(str);
   1.142 +    str.concat(str);
   1.143 +    str.concat(str);
   1.144 +});
   1.145 +
   1.146 +bench("trim", function() {
   1.147 +    str.trim();
   1.148 +    str.trim();
   1.149 +    str.trim();
   1.150 +});
   1.151 +
   1.152 +bench("toUpperCase", function() {
   1.153 +    str.toUpperCase();
   1.154 +});
   1.155 +
   1.156 +bench("toLowerCase", function() {
   1.157 +    str.toLowerCase();
   1.158 +});
   1.159 +
   1.160 +bench("valueOf", function() {
   1.161 +    str.valueOf();
   1.162 +    str.valueOf();
   1.163 +    str.valueOf();
   1.164 +});
   1.165 +
   1.166 +bench("toString", function() {
   1.167 +    str.toString();
   1.168 +    str.toString();
   1.169 +    str.toString();
   1.170 +});
   1.171 +
   1.172 +bench("String", function() {
   1.173 +    String(str);
   1.174 +    String(str);
   1.175 +    String(str);
   1.176 +});
   1.177 +
   1.178 +bench("new String", function() {
   1.179 +    new String(str);
   1.180 +    new String(str);
   1.181 +    new String(str);
   1.182 +});

mercurial