11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common.js'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst types = [ 51cb0ef41Sopenharmony_ci 'BigUInt64LE', 61cb0ef41Sopenharmony_ci 'BigUInt64BE', 71cb0ef41Sopenharmony_ci 'BigInt64LE', 81cb0ef41Sopenharmony_ci 'BigInt64BE', 91cb0ef41Sopenharmony_ci 'UInt8', 101cb0ef41Sopenharmony_ci 'UInt16LE', 111cb0ef41Sopenharmony_ci 'UInt16BE', 121cb0ef41Sopenharmony_ci 'UInt32LE', 131cb0ef41Sopenharmony_ci 'UInt32BE', 141cb0ef41Sopenharmony_ci 'UIntLE', 151cb0ef41Sopenharmony_ci 'UIntBE', 161cb0ef41Sopenharmony_ci 'Int8', 171cb0ef41Sopenharmony_ci 'Int16LE', 181cb0ef41Sopenharmony_ci 'Int16BE', 191cb0ef41Sopenharmony_ci 'Int32LE', 201cb0ef41Sopenharmony_ci 'Int32BE', 211cb0ef41Sopenharmony_ci 'IntLE', 221cb0ef41Sopenharmony_ci 'IntBE', 231cb0ef41Sopenharmony_ci 'FloatLE', 241cb0ef41Sopenharmony_ci 'FloatBE', 251cb0ef41Sopenharmony_ci 'DoubleLE', 261cb0ef41Sopenharmony_ci 'DoubleBE', 271cb0ef41Sopenharmony_ci]; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 301cb0ef41Sopenharmony_ci buffer: ['fast'], 311cb0ef41Sopenharmony_ci type: types, 321cb0ef41Sopenharmony_ci n: [1e6], 331cb0ef41Sopenharmony_ci}); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciconst INT8 = 0x7f; 361cb0ef41Sopenharmony_ciconst INT16 = 0x7fff; 371cb0ef41Sopenharmony_ciconst INT32 = 0x7fffffff; 381cb0ef41Sopenharmony_ciconst INT48 = 0x7fffffffffff; 391cb0ef41Sopenharmony_ciconst INT64 = 0x7fffffffffffffffn; 401cb0ef41Sopenharmony_ciconst UINT8 = 0xff; 411cb0ef41Sopenharmony_ciconst UINT16 = 0xffff; 421cb0ef41Sopenharmony_ciconst UINT32 = 0xffffffff; 431cb0ef41Sopenharmony_ciconst UINT64 = 0xffffffffffffffffn; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciconst mod = { 461cb0ef41Sopenharmony_ci writeBigInt64BE: INT64, 471cb0ef41Sopenharmony_ci writeBigInt64LE: INT64, 481cb0ef41Sopenharmony_ci writeBigUInt64BE: UINT64, 491cb0ef41Sopenharmony_ci writeBigUInt64LE: UINT64, 501cb0ef41Sopenharmony_ci writeInt8: INT8, 511cb0ef41Sopenharmony_ci writeInt16BE: INT16, 521cb0ef41Sopenharmony_ci writeInt16LE: INT16, 531cb0ef41Sopenharmony_ci writeInt32BE: INT32, 541cb0ef41Sopenharmony_ci writeInt32LE: INT32, 551cb0ef41Sopenharmony_ci writeUInt8: UINT8, 561cb0ef41Sopenharmony_ci writeUInt16BE: UINT16, 571cb0ef41Sopenharmony_ci writeUInt16LE: UINT16, 581cb0ef41Sopenharmony_ci writeUInt32BE: UINT32, 591cb0ef41Sopenharmony_ci writeUInt32LE: UINT32, 601cb0ef41Sopenharmony_ci writeUIntLE: INT8, 611cb0ef41Sopenharmony_ci writeUIntBE: INT16, 621cb0ef41Sopenharmony_ci writeIntLE: INT32, 631cb0ef41Sopenharmony_ci writeIntBE: INT48, 641cb0ef41Sopenharmony_ci}; 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciconst byteLength = { 671cb0ef41Sopenharmony_ci writeUIntLE: 1, 681cb0ef41Sopenharmony_ci writeUIntBE: 2, 691cb0ef41Sopenharmony_ci writeIntLE: 4, 701cb0ef41Sopenharmony_ci writeIntBE: 6, 711cb0ef41Sopenharmony_ci}; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_cifunction main({ n, buf, type }) { 741cb0ef41Sopenharmony_ci const buff = buf === 'fast' ? 751cb0ef41Sopenharmony_ci Buffer.alloc(8) : 761cb0ef41Sopenharmony_ci require('buffer').SlowBuffer(8); 771cb0ef41Sopenharmony_ci const fn = `write${type}`; 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci if (!/\d/.test(fn)) 801cb0ef41Sopenharmony_ci benchSpecialInt(buff, fn, n); 811cb0ef41Sopenharmony_ci else if (/BigU?Int/.test(fn)) 821cb0ef41Sopenharmony_ci benchBigInt(buff, fn, BigInt(n)); 831cb0ef41Sopenharmony_ci else if (/Int/.test(fn)) 841cb0ef41Sopenharmony_ci benchInt(buff, fn, n); 851cb0ef41Sopenharmony_ci else 861cb0ef41Sopenharmony_ci benchFloat(buff, fn, n); 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_cifunction benchBigInt(buff, fn, n) { 901cb0ef41Sopenharmony_ci const m = mod[fn]; 911cb0ef41Sopenharmony_ci bench.start(); 921cb0ef41Sopenharmony_ci for (let i = 0n; i !== n; i++) { 931cb0ef41Sopenharmony_ci buff[fn](i & m, 0); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci bench.end(Number(n)); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_cifunction benchInt(buff, fn, n) { 991cb0ef41Sopenharmony_ci const m = mod[fn]; 1001cb0ef41Sopenharmony_ci bench.start(); 1011cb0ef41Sopenharmony_ci for (let i = 0; i !== n; i++) { 1021cb0ef41Sopenharmony_ci buff[fn](i & m, 0); 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci bench.end(n); 1051cb0ef41Sopenharmony_ci} 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_cifunction benchSpecialInt(buff, fn, n) { 1081cb0ef41Sopenharmony_ci const m = mod[fn]; 1091cb0ef41Sopenharmony_ci const byte = byteLength[fn]; 1101cb0ef41Sopenharmony_ci bench.start(); 1111cb0ef41Sopenharmony_ci for (let i = 0; i !== n; i++) { 1121cb0ef41Sopenharmony_ci buff[fn](i & m, 0, byte); 1131cb0ef41Sopenharmony_ci } 1141cb0ef41Sopenharmony_ci bench.end(n); 1151cb0ef41Sopenharmony_ci} 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_cifunction benchFloat(buff, fn, n) { 1181cb0ef41Sopenharmony_ci bench.start(); 1191cb0ef41Sopenharmony_ci for (let i = 0; i !== n; i++) { 1201cb0ef41Sopenharmony_ci buff[fn](i, 0); 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci bench.end(n); 1231cb0ef41Sopenharmony_ci} 124