File: bytestring_perf.js

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * @license
 * Copyright The Closure Library Authors.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @fileoverview Performance test for different implementations of
 * byteArrayToString.
 */


goog.provide('goog.crypt.byteArrayToStringPerf');

goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.testing.PerformanceTable');

goog.setTestOnly('goog.crypt.byteArrayToStringPerf');


var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));


var BYTES_LENGTH = Math.pow(2, 20);
var CHUNK_SIZE = 8192;

function getBytes() {
  var bytes = [];
  for (var i = 0; i < BYTES_LENGTH; i++) {
    bytes.push('A'.charCodeAt(0));
  }
  return bytes;
}

function copyAndSpliceByteArray(bytes) {
  // Copy the passed byte array since we're going to destroy it.
  var remainingBytes = goog.array.clone(bytes);
  var strings = [];

  // Convert each chunk to a string.
  while (remainingBytes.length) {
    var chunk = remainingBytes.splice(0, CHUNK_SIZE);
    strings.push(String.fromCharCode.apply(null, chunk));
  }
  return strings.join('');
}

function sliceByteArrayConcat(bytes) {
  var str = '';
  for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
    var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
    str += String.fromCharCode.apply(null, chunk);
  }
  return str;
}


function sliceByteArrayJoin(bytes) {
  var strings = [];
  for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
    var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
    strings.push(String.fromCharCode.apply(null, chunk));
  }
  return strings.join('');
}

function mapByteArray(bytes) {
  var strings = goog.array.map(bytes, String.fromCharCode);
  return strings.join('');
}

function forLoopByteArrayConcat(bytes) {
  var str = '';
  for (var i = 0; i < bytes.length; i++) {
    str += String.fromCharCode(bytes[i]);
  }
  return str;
}

function forLoopByteArrayJoin(bytes) {
  var strs = [];
  for (var i = 0; i < bytes.length; i++) {
    strs.push(String.fromCharCode(bytes[i]));
  }
  return strs.join('');
}


function run() {
  var bytes = getBytes();
  table.run(
      goog.partial(copyAndSpliceByteArray, getBytes()),
      'Copy array and splice out chunks.');

  table.run(
      goog.partial(sliceByteArrayConcat, getBytes()),
      'Slice out copies of the byte array, concatenating results');

  table.run(
      goog.partial(sliceByteArrayJoin, getBytes()),
      'Slice out copies of the byte array, joining results');

  table.run(
      goog.partial(forLoopByteArrayConcat, getBytes()),
      'Use for loop with concat.');

  table.run(
      goog.partial(forLoopByteArrayJoin, getBytes()),
      'Use for loop with join.');

  // Purposefully commented out. This ends up being tremendously expensive.
  // table.run(goog.partial(mapByteArray, getBytes()),
  //           'Use goog.array.map and fromCharCode.');
}

run();