Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/crypt/bytestring_perf.js
2868 views
1
// Copyright 2014 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Performance test for different implementations of
17
* byteArrayToString.
18
*/
19
20
21
goog.provide('goog.crypt.byteArrayToStringPerf');
22
23
goog.require('goog.array');
24
goog.require('goog.dom');
25
goog.require('goog.testing.PerformanceTable');
26
27
goog.setTestOnly('goog.crypt.byteArrayToStringPerf');
28
29
30
var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));
31
32
33
var BYTES_LENGTH = Math.pow(2, 20);
34
var CHUNK_SIZE = 8192;
35
36
function getBytes() {
37
var bytes = [];
38
for (var i = 0; i < BYTES_LENGTH; i++) {
39
bytes.push('A'.charCodeAt(0));
40
}
41
return bytes;
42
}
43
44
function copyAndSpliceByteArray(bytes) {
45
// Copy the passed byte array since we're going to destroy it.
46
var remainingBytes = goog.array.clone(bytes);
47
var strings = [];
48
49
// Convert each chunk to a string.
50
while (remainingBytes.length) {
51
var chunk = goog.array.splice(remainingBytes, 0, CHUNK_SIZE);
52
strings.push(String.fromCharCode.apply(null, chunk));
53
}
54
return strings.join('');
55
}
56
57
function sliceByteArrayConcat(bytes) {
58
var str = '';
59
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
60
var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
61
str += String.fromCharCode.apply(null, chunk);
62
}
63
return str;
64
}
65
66
67
function sliceByteArrayJoin(bytes) {
68
var strings = [];
69
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
70
var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
71
strings.push(String.fromCharCode.apply(null, chunk));
72
}
73
return strings.join('');
74
}
75
76
function mapByteArray(bytes) {
77
var strings = goog.array.map(bytes, String.fromCharCode);
78
return strings.join('');
79
}
80
81
function forLoopByteArrayConcat(bytes) {
82
var str = '';
83
for (var i = 0; i < bytes.length; i++) {
84
str += String.fromCharCode(bytes[i]);
85
}
86
return str;
87
}
88
89
function forLoopByteArrayJoin(bytes) {
90
var strs = [];
91
for (var i = 0; i < bytes.length; i++) {
92
strs.push(String.fromCharCode(bytes[i]));
93
}
94
return strs.join('');
95
}
96
97
98
function run() {
99
var bytes = getBytes();
100
table.run(
101
goog.partial(copyAndSpliceByteArray, getBytes()),
102
'Copy array and splice out chunks.');
103
104
table.run(
105
goog.partial(sliceByteArrayConcat, getBytes()),
106
'Slice out copies of the byte array, concatenating results');
107
108
table.run(
109
goog.partial(sliceByteArrayJoin, getBytes()),
110
'Slice out copies of the byte array, joining results');
111
112
table.run(
113
goog.partial(forLoopByteArrayConcat, getBytes()),
114
'Use for loop with concat.');
115
116
table.run(
117
goog.partial(forLoopByteArrayJoin, getBytes()),
118
'Use for loop with join.');
119
120
// Purposefully commented out. This ends up being tremendously expensive.
121
// table.run(goog.partial(mapByteArray, getBytes()),
122
// 'Use goog.array.map and fromCharCode.');
123
}
124
125
run();
126
127