OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 new BenchmarkSuite('StringFunctions', [1000], [ | 5 new BenchmarkSuite('StringFunctions', [1000], [ |
6 new Benchmark('StringRepeat', false, false, 0, | 6 new Benchmark('StringRepeat', false, false, 0, |
7 StringRepeat, StringRepeatSetup, StringRepeatTearDown), | 7 Repeat, RepeatSetup, RepeatTearDown), |
8 new Benchmark('StringStartsWith', false, false, 0, | 8 new Benchmark('StringStartsWith', false, false, 0, |
9 StringStartsWith, StringWithSetup, StringWithTearDown), | 9 StartsWith, WithSetup, WithTearDown), |
10 new Benchmark('StringEndsWith', false, false, 0, | 10 new Benchmark('StringEndsWith', false, false, 0, |
11 StringEndsWith, StringWithSetup, StringWithTearDown), | 11 EndsWith, WithSetup, WithTearDown), |
12 new Benchmark('StringContains', false, false, 0, | 12 new Benchmark('StringContains', false, false, 0, |
13 StringContains, StringContainsSetup, StringWithTearDown), | 13 Contains, ContainsSetup, WithTearDown), |
| 14 new Benchmark('StringFromCodePoint', false, false, 0, |
| 15 FromCodePoint, FromCodePointSetup, FromCodePointTearDown), |
| 16 new Benchmark('StringCodePointAt', false, false, 0, |
| 17 CodePointAt, CodePointAtSetup, CodePointAtTearDown), |
14 ]); | 18 ]); |
15 | 19 |
16 | 20 |
17 var result; | 21 var result; |
18 | 22 |
19 var stringRepeatSource = "abc"; | 23 var stringRepeatSource = "abc"; |
20 | 24 |
21 function StringRepeatSetup() { | 25 function RepeatSetup() { |
22 result = undefined; | 26 result = undefined; |
23 } | 27 } |
24 | 28 |
25 function StringRepeat() { | 29 function Repeat() { |
26 result = stringRepeatSource.repeat(500); | 30 result = stringRepeatSource.repeat(500); |
27 } | 31 } |
28 | 32 |
29 function StringRepeatTearDown() { | 33 function RepeatTearDown() { |
30 var expected = ""; | 34 var expected = ""; |
31 for(var i = 0; i < 1000; i++) { | 35 for(var i = 0; i < 1000; i++) { |
32 expected += stringRepeatSource; | 36 expected += stringRepeatSource; |
33 } | 37 } |
34 return result === expected; | 38 return result === expected; |
35 } | 39 } |
36 | 40 |
37 | 41 |
38 var str; | 42 var str; |
39 var substr; | 43 var substr; |
40 | 44 |
41 function StringWithSetup() { | 45 function WithSetup() { |
42 str = "abc".repeat(500); | 46 str = "abc".repeat(500); |
43 substr = "abc".repeat(200); | 47 substr = "abc".repeat(200); |
44 result = undefined; | 48 result = undefined; |
45 } | 49 } |
46 | 50 |
47 function StringWithTearDown() { | 51 function WithTearDown() { |
48 return !!result; | 52 return !!result; |
49 } | 53 } |
50 | 54 |
51 function StringStartsWith() { | 55 function StartsWith() { |
52 result = str.startsWith(substr); | 56 result = str.startsWith(substr); |
53 } | 57 } |
54 | 58 |
55 function StringEndsWith() { | 59 function EndsWith() { |
56 result = str.endsWith(substr); | 60 result = str.endsWith(substr); |
57 } | 61 } |
58 | 62 |
59 function StringContainsSetup() { | 63 function ContainsSetup() { |
60 str = "def".repeat(100) + "abc".repeat(100) + "qqq".repeat(100); | 64 str = "def".repeat(100) + "abc".repeat(100) + "qqq".repeat(100); |
61 substr = "abc".repeat(100); | 65 substr = "abc".repeat(100); |
62 } | 66 } |
63 | 67 |
64 function StringContains() { | 68 function Contains() { |
65 result = str.contains(substr); | 69 result = str.contains(substr); |
66 } | 70 } |
| 71 |
| 72 var MAX_CODE_POINT = 0xFFFFF; |
| 73 |
| 74 function FromCodePointSetup() { |
| 75 result = new Array(MAX_CODE_POINT + 1); |
| 76 } |
| 77 |
| 78 function FromCodePoint() { |
| 79 for (var i = 0; i <= MAX_CODE_POINT; i++) { |
| 80 result[i] = String.fromCodePoint(i); |
| 81 } |
| 82 } |
| 83 |
| 84 function FromCodePointTearDown() { |
| 85 for (var i = 0; i <= MAX_CODE_POINT; i++) { |
| 86 if (i !== result[i].codePointAt(0)) return false; |
| 87 } |
| 88 return true; |
| 89 } |
| 90 |
| 91 |
| 92 var allCodePoints; |
| 93 |
| 94 function CodePointAtSetup() { |
| 95 allCodePoints = new Array(MAX_CODE_POINT + 1); |
| 96 for (var i = 0; i <= MAX_CODE_POINT; i++) { |
| 97 allCodePoints = String.fromCodePoint(i); |
| 98 } |
| 99 result = undefined; |
| 100 } |
| 101 |
| 102 function CodePointAt() { |
| 103 result = 0; |
| 104 for (var i = 0; i <= MAX_CODE_POINT; i++) { |
| 105 result += allCodePoints.codePointAt(i); |
| 106 } |
| 107 } |
| 108 |
| 109 function CodePointAtTearDown() { |
| 110 return result === MAX_CODE_POINT * (MAX_CODE_POINT + 1) / 2; |
| 111 } |
OLD | NEW |