11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 61cb0ef41Sopenharmony_ci source: [ 71cb0ef41Sopenharmony_ci 'array', 81cb0ef41Sopenharmony_ci 'arraybuffer', 91cb0ef41Sopenharmony_ci 'arraybuffer-middle', 101cb0ef41Sopenharmony_ci 'buffer', 111cb0ef41Sopenharmony_ci 'string', 121cb0ef41Sopenharmony_ci 'string-utf8', 131cb0ef41Sopenharmony_ci 'string-base64', 141cb0ef41Sopenharmony_ci 'object', 151cb0ef41Sopenharmony_ci 'uint8array', 161cb0ef41Sopenharmony_ci 'uint16array', 171cb0ef41Sopenharmony_ci ], 181cb0ef41Sopenharmony_ci len: [100, 2048], 191cb0ef41Sopenharmony_ci n: [8e5], 201cb0ef41Sopenharmony_ci}); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cifunction main({ len, n, source }) { 231cb0ef41Sopenharmony_ci let i = 0; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci switch (source) { 261cb0ef41Sopenharmony_ci case 'array': { 271cb0ef41Sopenharmony_ci const array = new Array(len).fill(42); 281cb0ef41Sopenharmony_ci bench.start(); 291cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 301cb0ef41Sopenharmony_ci Buffer.from(array); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci bench.end(n); 331cb0ef41Sopenharmony_ci break; 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci case 'arraybuffer': { 361cb0ef41Sopenharmony_ci const arrayBuf = new ArrayBuffer(len); 371cb0ef41Sopenharmony_ci bench.start(); 381cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 391cb0ef41Sopenharmony_ci Buffer.from(arrayBuf); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci bench.end(n); 421cb0ef41Sopenharmony_ci break; 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci case 'arraybuffer-middle': { 451cb0ef41Sopenharmony_ci const arrayBuf = new ArrayBuffer(len); 461cb0ef41Sopenharmony_ci const offset = ~~(len / 4); 471cb0ef41Sopenharmony_ci const length = ~~(len / 2); 481cb0ef41Sopenharmony_ci bench.start(); 491cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 501cb0ef41Sopenharmony_ci Buffer.from(arrayBuf, offset, length); 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci bench.end(n); 531cb0ef41Sopenharmony_ci break; 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci case 'buffer': { 561cb0ef41Sopenharmony_ci const buffer = Buffer.allocUnsafe(len); 571cb0ef41Sopenharmony_ci bench.start(); 581cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 591cb0ef41Sopenharmony_ci Buffer.from(buffer); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci bench.end(n); 621cb0ef41Sopenharmony_ci break; 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci case 'uint8array': { 651cb0ef41Sopenharmony_ci const uint8array = new Uint8Array(len); 661cb0ef41Sopenharmony_ci bench.start(); 671cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 681cb0ef41Sopenharmony_ci Buffer.from(uint8array); 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci bench.end(n); 711cb0ef41Sopenharmony_ci break; 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci case 'uint16array': { 741cb0ef41Sopenharmony_ci const uint16array = new Uint16Array(len); 751cb0ef41Sopenharmony_ci bench.start(); 761cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 771cb0ef41Sopenharmony_ci Buffer.from(uint16array); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci bench.end(n); 801cb0ef41Sopenharmony_ci break; 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci case 'string': { 831cb0ef41Sopenharmony_ci const str = 'a'.repeat(len); 841cb0ef41Sopenharmony_ci bench.start(); 851cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 861cb0ef41Sopenharmony_ci Buffer.from(str); 871cb0ef41Sopenharmony_ci } 881cb0ef41Sopenharmony_ci bench.end(n); 891cb0ef41Sopenharmony_ci break; 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci case 'string-utf8': { 921cb0ef41Sopenharmony_ci const str = 'a'.repeat(len); 931cb0ef41Sopenharmony_ci bench.start(); 941cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 951cb0ef41Sopenharmony_ci Buffer.from(str, 'utf8'); 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci bench.end(n); 981cb0ef41Sopenharmony_ci break; 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci case 'string-base64': { 1011cb0ef41Sopenharmony_ci const str = 'a'.repeat(len); 1021cb0ef41Sopenharmony_ci bench.start(); 1031cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 1041cb0ef41Sopenharmony_ci Buffer.from(str, 'base64'); 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci bench.end(n); 1071cb0ef41Sopenharmony_ci break; 1081cb0ef41Sopenharmony_ci } 1091cb0ef41Sopenharmony_ci case 'object': { 1101cb0ef41Sopenharmony_ci const obj = { length: null }; // Results in a new, empty Buffer 1111cb0ef41Sopenharmony_ci bench.start(); 1121cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 1131cb0ef41Sopenharmony_ci Buffer.from(obj); 1141cb0ef41Sopenharmony_ci } 1151cb0ef41Sopenharmony_ci bench.end(n); 1161cb0ef41Sopenharmony_ci break; 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci default: 1191cb0ef41Sopenharmony_ci assert.fail('Should not get here'); 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci} 122