1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 33var str = "The quick gray nashorn jumps over the lazy zebra."; 34 35function bench(name, func) { 36 var start = Date.now(); 37 for (var iter = 0; iter < 5000000; iter++) { 38 func(); 39 } 40 print((Date.now() - start) + "\t" + name); 41} 42 43bench("[]", function() { 44 str[0]; 45 str[1]; 46 str[2]; 47}); 48 49bench("fromCharCode 1", function() { 50 String.fromCharCode(97); 51 String.fromCharCode(98); 52 String.fromCharCode(99); 53}); 54 55bench("fromCharCode 2", function() { 56 String.fromCharCode(97, 98); 57 String.fromCharCode(97, 98, 99); 58 String.fromCharCode(97, 98, 99, 100); 59}); 60 61bench("charAt 1", function() { 62 str.charAt(0); 63 str.charAt(1); 64 str.charAt(2); 65}); 66 67bench("charAt 2", function() { 68 str.charAt(100); 69 str.charAt(-1); 70}); 71 72bench("charCodeAt 1", function() { 73 str.charCodeAt(0); 74 str.charCodeAt(1); 75 str.charCodeAt(2); 76}); 77 78bench("charCodeAt 2", function() { 79 str.charCodeAt(100); 80 str.charCodeAt(-1); 81}); 82 83bench("indexOf 1", function() { 84 str.indexOf("T"); 85 str.indexOf("h"); 86 str.indexOf("e"); 87}); 88 89bench("indexOf 2", function() { 90 str.indexOf("T", 0); 91 str.indexOf("h", 1); 92 str.indexOf("e", 2); 93}); 94 95bench("lastIndexOf", function() { 96 str.indexOf("a"); 97 str.indexOf("r"); 98 str.indexOf("b"); 99}); 100 101bench("slice", function() { 102 str.slice(5); 103 str.slice(5); 104 str.slice(5); 105}); 106 107bench("split 1", function() { 108 str.split(); 109}); 110 111bench("split 2", function() { 112 str.split("foo"); 113}); 114 115bench("split 3", function() { 116 str.split(/foo/); 117}); 118 119bench("substring 1", function() { 120 str.substring(0); 121 str.substring(0); 122 str.substring(0); 123}); 124 125bench("substring 2", function() { 126 str.substring(0, 5); 127 str.substring(0, 5); 128 str.substring(0, 5); 129}); 130 131bench("substr", function() { 132 str.substr(0); 133 str.substr(0); 134 str.substr(0); 135}); 136 137bench("slice", function() { 138 str.slice(0); 139 str.slice(0); 140 str.slice(0); 141}); 142 143bench("concat", function() { 144 str.concat(str); 145 str.concat(str); 146 str.concat(str); 147}); 148 149bench("trim", function() { 150 str.trim(); 151 str.trim(); 152 str.trim(); 153}); 154 155bench("toUpperCase", function() { 156 str.toUpperCase(); 157}); 158 159bench("toLowerCase", function() { 160 str.toLowerCase(); 161}); 162 163bench("valueOf", function() { 164 str.valueOf(); 165 str.valueOf(); 166 str.valueOf(); 167}); 168 169bench("toString", function() { 170 str.toString(); 171 str.toString(); 172 str.toString(); 173}); 174 175bench("String", function() { 176 String(str); 177 String(str); 178 String(str); 179}); 180 181bench("new String", function() { 182 new String(str); 183 new String(str); 184 new String(str); 185}); 186