bytestring_perf.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * @fileoverview Performance test for different implementations of
  16. * byteArrayToString.
  17. */
  18. goog.provide('goog.crypt.byteArrayToStringPerf');
  19. goog.require('goog.array');
  20. goog.require('goog.dom');
  21. goog.require('goog.testing.PerformanceTable');
  22. goog.setTestOnly('goog.crypt.byteArrayToStringPerf');
  23. var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));
  24. var BYTES_LENGTH = Math.pow(2, 20);
  25. var CHUNK_SIZE = 8192;
  26. function getBytes() {
  27. var bytes = [];
  28. for (var i = 0; i < BYTES_LENGTH; i++) {
  29. bytes.push('A'.charCodeAt(0));
  30. }
  31. return bytes;
  32. }
  33. function copyAndSpliceByteArray(bytes) {
  34. // Copy the passed byte array since we're going to destroy it.
  35. var remainingBytes = goog.array.clone(bytes);
  36. var strings = [];
  37. // Convert each chunk to a string.
  38. while (remainingBytes.length) {
  39. var chunk = goog.array.splice(remainingBytes, 0, CHUNK_SIZE);
  40. strings.push(String.fromCharCode.apply(null, chunk));
  41. }
  42. return strings.join('');
  43. }
  44. function sliceByteArrayConcat(bytes) {
  45. var str = '';
  46. for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
  47. var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
  48. str += String.fromCharCode.apply(null, chunk);
  49. }
  50. return str;
  51. }
  52. function sliceByteArrayJoin(bytes) {
  53. var strings = [];
  54. for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
  55. var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
  56. strings.push(String.fromCharCode.apply(null, chunk));
  57. }
  58. return strings.join('');
  59. }
  60. function mapByteArray(bytes) {
  61. var strings = goog.array.map(bytes, String.fromCharCode);
  62. return strings.join('');
  63. }
  64. function forLoopByteArrayConcat(bytes) {
  65. var str = '';
  66. for (var i = 0; i < bytes.length; i++) {
  67. str += String.fromCharCode(bytes[i]);
  68. }
  69. return str;
  70. }
  71. function forLoopByteArrayJoin(bytes) {
  72. var strs = [];
  73. for (var i = 0; i < bytes.length; i++) {
  74. strs.push(String.fromCharCode(bytes[i]));
  75. }
  76. return strs.join('');
  77. }
  78. function run() {
  79. var bytes = getBytes();
  80. table.run(
  81. goog.partial(copyAndSpliceByteArray, getBytes()),
  82. 'Copy array and splice out chunks.');
  83. table.run(
  84. goog.partial(sliceByteArrayConcat, getBytes()),
  85. 'Slice out copies of the byte array, concatenating results');
  86. table.run(
  87. goog.partial(sliceByteArrayJoin, getBytes()),
  88. 'Slice out copies of the byte array, joining results');
  89. table.run(
  90. goog.partial(forLoopByteArrayConcat, getBytes()),
  91. 'Use for loop with concat.');
  92. table.run(
  93. goog.partial(forLoopByteArrayJoin, getBytes()),
  94. 'Use for loop with join.');
  95. // Purposefully commented out. This ends up being tremendously expensive.
  96. // table.run(goog.partial(mapByteArray, getBytes()),
  97. // 'Use goog.array.map and fromCharCode.');
  98. }
  99. run();