Tue, 16 Jan 2018 23:10:07 -0800
Merge
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 */
33 var str = "The quick gray nashorn jumps over the lazy zebra.";
35 function 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 }
43 bench("[]", function() {
44 str[0];
45 str[1];
46 str[2];
47 });
49 bench("fromCharCode 1", function() {
50 String.fromCharCode(97);
51 String.fromCharCode(98);
52 String.fromCharCode(99);
53 });
55 bench("fromCharCode 2", function() {
56 String.fromCharCode(97, 98);
57 String.fromCharCode(97, 98, 99);
58 String.fromCharCode(97, 98, 99, 100);
59 });
61 bench("charAt 1", function() {
62 str.charAt(0);
63 str.charAt(1);
64 str.charAt(2);
65 });
67 bench("charAt 2", function() {
68 str.charAt(100);
69 str.charAt(-1);
70 });
72 bench("charCodeAt 1", function() {
73 str.charCodeAt(0);
74 str.charCodeAt(1);
75 str.charCodeAt(2);
76 });
78 bench("charCodeAt 2", function() {
79 str.charCodeAt(100);
80 str.charCodeAt(-1);
81 });
83 bench("indexOf 1", function() {
84 str.indexOf("T");
85 str.indexOf("h");
86 str.indexOf("e");
87 });
89 bench("indexOf 2", function() {
90 str.indexOf("T", 0);
91 str.indexOf("h", 1);
92 str.indexOf("e", 2);
93 });
95 bench("lastIndexOf", function() {
96 str.indexOf("a");
97 str.indexOf("r");
98 str.indexOf("b");
99 });
101 bench("slice", function() {
102 str.slice(5);
103 str.slice(5);
104 str.slice(5);
105 });
107 bench("split 1", function() {
108 str.split();
109 });
111 bench("split 2", function() {
112 str.split("foo");
113 });
115 bench("split 3", function() {
116 str.split(/foo/);
117 });
119 bench("substring 1", function() {
120 str.substring(0);
121 str.substring(0);
122 str.substring(0);
123 });
125 bench("substring 2", function() {
126 str.substring(0, 5);
127 str.substring(0, 5);
128 str.substring(0, 5);
129 });
131 bench("substr", function() {
132 str.substr(0);
133 str.substr(0);
134 str.substr(0);
135 });
137 bench("slice", function() {
138 str.slice(0);
139 str.slice(0);
140 str.slice(0);
141 });
143 bench("concat", function() {
144 str.concat(str);
145 str.concat(str);
146 str.concat(str);
147 });
149 bench("trim", function() {
150 str.trim();
151 str.trim();
152 str.trim();
153 });
155 bench("toUpperCase", function() {
156 str.toUpperCase();
157 });
159 bench("toLowerCase", function() {
160 str.toLowerCase();
161 });
163 bench("valueOf", function() {
164 str.valueOf();
165 str.valueOf();
166 str.valueOf();
167 });
169 bench("toString", function() {
170 str.toString();
171 str.toString();
172 str.toString();
173 });
175 bench("String", function() {
176 String(str);
177 String(str);
178 String(str);
179 });
181 bench("new String", function() {
182 new String(str);
183 new String(str);
184 new String(str);
185 });