Path: blob/trunk/third_party/closure/goog/crypt/bytestring_perf.js
2868 views
// Copyright 2014 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Performance test for different implementations of16* byteArrayToString.17*/181920goog.provide('goog.crypt.byteArrayToStringPerf');2122goog.require('goog.array');23goog.require('goog.dom');24goog.require('goog.testing.PerformanceTable');2526goog.setTestOnly('goog.crypt.byteArrayToStringPerf');272829var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable'));303132var BYTES_LENGTH = Math.pow(2, 20);33var CHUNK_SIZE = 8192;3435function getBytes() {36var bytes = [];37for (var i = 0; i < BYTES_LENGTH; i++) {38bytes.push('A'.charCodeAt(0));39}40return bytes;41}4243function copyAndSpliceByteArray(bytes) {44// Copy the passed byte array since we're going to destroy it.45var remainingBytes = goog.array.clone(bytes);46var strings = [];4748// Convert each chunk to a string.49while (remainingBytes.length) {50var chunk = goog.array.splice(remainingBytes, 0, CHUNK_SIZE);51strings.push(String.fromCharCode.apply(null, chunk));52}53return strings.join('');54}5556function sliceByteArrayConcat(bytes) {57var str = '';58for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {59var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);60str += String.fromCharCode.apply(null, chunk);61}62return str;63}646566function sliceByteArrayJoin(bytes) {67var strings = [];68for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {69var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);70strings.push(String.fromCharCode.apply(null, chunk));71}72return strings.join('');73}7475function mapByteArray(bytes) {76var strings = goog.array.map(bytes, String.fromCharCode);77return strings.join('');78}7980function forLoopByteArrayConcat(bytes) {81var str = '';82for (var i = 0; i < bytes.length; i++) {83str += String.fromCharCode(bytes[i]);84}85return str;86}8788function forLoopByteArrayJoin(bytes) {89var strs = [];90for (var i = 0; i < bytes.length; i++) {91strs.push(String.fromCharCode(bytes[i]));92}93return strs.join('');94}959697function run() {98var bytes = getBytes();99table.run(100goog.partial(copyAndSpliceByteArray, getBytes()),101'Copy array and splice out chunks.');102103table.run(104goog.partial(sliceByteArrayConcat, getBytes()),105'Slice out copies of the byte array, concatenating results');106107table.run(108goog.partial(sliceByteArrayJoin, getBytes()),109'Slice out copies of the byte array, joining results');110111table.run(112goog.partial(forLoopByteArrayConcat, getBytes()),113'Use for loop with concat.');114115table.run(116goog.partial(forLoopByteArrayJoin, getBytes()),117'Use for loop with join.');118119// Purposefully commented out. This ends up being tremendously expensive.120// table.run(goog.partial(mapByteArray, getBytes()),121// 'Use goog.array.map and fromCharCode.');122}123124run();125126127