why is the performance of javascript bitwise operations unstable All In One
why is the performance of javascript bitwise operations unstable All In One
为什么 javascript
位运算
的性能不稳定 ?
性能优化/性能测试
demos
~~
doublenot bitwise
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-07-22
* @modified 2022-09-27
* @modified 2023-02-25
*
* @description js performance.now / console.time & console.timeEnd
* @description js 高精度时间戳 / 性能优化 / 性能测试
* @difficulty Easy
* @time_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/16736458.html
* @link https://www.cnblogs.com/xgqfrms/p/13360113.html
* @solutions
*
* @best_solutions
*
*/
// export {};
// const log = console.log;
function math(n) {
const nums = [];
let startTime = performance.now();
for (let i = 0; i < n; i++) {
nums.push(Math.floor(Math.random()));
}
let endTime = performance.now();
console.log(`🐌 Math.floor cost time is ${endTime - startTime} ms`, n);
return endTime - startTime;
}
function bitwise(n) {
const nums = [];
let startTime = performance.now();
for (let i = 0; i < n; i++) {
nums.push(~~Math.random());
}
let endTime = performance.now();
console.log(`🚀 double not bitwise cost time is ${endTime - startTime} ms`, n);
return endTime - startTime;
}
// test cases
const arr = [...``.padEnd(8, `_`)].map((_, i) => i + 1);
let mathCost;
let bitwiseCosts;
for (const n of arr) {
console.log(`\n n =`, n);
mathCost = math(10**n);
bitwiseCosts = bitwise(10**n);
if(mathCost > bitwiseCosts) {
console.log(`🚀 bitwiseCosts performance ✅`, `${mathCost - bitwiseCosts} ms`);
} else {
console.log(`🚀 bitwiseCosts performance ❌`, `${bitwiseCosts - mathCost} ms`);
}
}
test env
// Chrome DevTools
// Version 109.0.5414.119 (Official Build) (x86_64)
$ node -v
# v18.12.0
$ node ./bitwise.js
$ npx ts-node -v
# v10.9.1
$ npx ts-node ./bitwise.js
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://gist.github.com/xgqfrms/b5cc537ecfeac1b1e64e4f5a82d8776e
https://github.com/microsoft/TypeScript/issues/52929
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17155780.html
未经授权禁止转载,违者必究!