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
3// found in the LICENSE file.
4
5new BenchmarkSuite('StringFunctions', [1000], [
6  new Benchmark('StringRepeat', false, false, 0,
7                Repeat, RepeatSetup, RepeatTearDown),
8  new Benchmark('StringStartsWith', false, false, 0,
9                StartsWith, WithSetup, WithTearDown),
10  new Benchmark('StringEndsWith', false, false, 0,
11                EndsWith, WithSetup, WithTearDown),
12  new Benchmark('StringIncludes', false, false, 0,
13                Includes, IncludesSetup, WithTearDown),
14  new Benchmark('StringFromCodePoint', false, false, 0,
15                FromCodePoint, FromCodePointSetup, FromCodePointTearDown),
16  new Benchmark('StringCodePointAt', false, false, 0,
17                CodePointAt, CodePointAtSetup, CodePointAtTearDown),
18]);
19
20
21var result;
22
23var stringRepeatSource = "abc";
24
25function RepeatSetup() {
26  result = undefined;
27}
28
29function Repeat() {
30  result = stringRepeatSource.repeat(500);
31}
32
33function RepeatTearDown() {
34  var expected = "";
35  for(var i = 0; i < 1000; i++) {
36    expected += stringRepeatSource;
37  }
38  return result === expected;
39}
40
41
42var str;
43var substr;
44
45function WithSetup() {
46  str = "abc".repeat(500);
47  substr = "abc".repeat(200);
48  result = undefined;
49}
50
51function WithTearDown() {
52  return !!result;
53}
54
55function StartsWith() {
56  result = str.startsWith(substr);
57}
58
59function EndsWith() {
60  result = str.endsWith(substr);
61}
62
63function IncludesSetup() {
64  str = "def".repeat(100) + "abc".repeat(100) + "qqq".repeat(100);
65  substr = "abc".repeat(100);
66}
67
68function Includes() {
69  result = str.includes(substr);
70}
71
72var MAX_CODE_POINT = 0xFFFFF;
73
74function FromCodePointSetup() {
75  result = new Array(MAX_CODE_POINT + 1);
76}
77
78function FromCodePoint() {
79  for (var i = 0; i <= MAX_CODE_POINT; i++) {
80    result[i] = String.fromCodePoint(i);
81  }
82}
83
84function 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
92var allCodePoints;
93
94function 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
102function CodePointAt() {
103  result = 0;
104  for (var i = 0; i <= MAX_CODE_POINT; i++) {
105    result += allCodePoints.codePointAt(i);
106  }
107}
108
109function CodePointAtTearDown() {
110  return result === MAX_CODE_POINT * (MAX_CODE_POINT + 1) / 2;
111}
112