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