首页 > 其他分享 >js performance checker All In One

js performance checker All In One

时间:2023-02-19 15:23:27浏览次数:44  
标签:console js checker let result ms performance const

js performance checker All In One

js 性能检测

  • console.time

  • console.timeLog

  • console.timeEnd

console.time(`⏰ performance`);
for(let i = 0; i < 10**3; i += 100) {
  console.timeLog(`⏰ performance`);
}
console.timeEnd(`⏰ performance`);

⏰ performance: 0.01123046875 ms
⏰ performance: 0.06103515625 ms
⏰ performance: 0.0869140625 ms
⏰ performance: 0.10009765625 ms
⏰ performance: 0.1142578125 ms
⏰ performance: 0.130126953125 ms
⏰ performance: 0.14697265625 ms
⏰ performance: 0.158935546875 ms
⏰ performance: 0.175048828125 ms
⏰ performance: 0.18603515625 ms
⏰ performance: 0.2021484375 ms

image

console.time(`⏰ performance`);

for(let i = 0; i < 10**3; i += 100) {
  console.log(`i =`, i);
}

console.timeEnd(`⏰ performance`);
// ⏰ performance: 0.166259765625 ms

image

performance.now

高精度时间戳

const t1 = performance.now();

for(let i = 0; i < 10**3; i += 100) {
  console.log(`i =`, i);
}

const t2 = performance.now();

console.log(`⏰ performance`, t2 - t1);
// 

image

const t1 = new Date();

for(let i = 0; i < 10**3; i += 100) {
  console.log(`i =`, i);
}

const t2 = new Date();

console.log(`⏰ performance`, t2 - t1);

image

https://developer.mozilla.org/en-US/docs/Web/API/Performance

demos


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2023-02-19
 * @modified
 *
 * @description 454. 四数相加 II
 * @description 454. 4Sum II
 * @difficulty Medium
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/4sum-ii/
 * @link https://leetcode.cn/problems/4sum-ii/
 * @solutions
 *
 * @best_solutions
 *
 */

// export {};

const log = console.log;

function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
  if(nums1.length === 1) {
    return nums1[0] + nums2[0] + nums3[0] + nums4[0] === 0 ? 1 : 0;
  }
  let num = 0;
  for(let i of nums1) {
    for(let ii of nums2) {
      for(let iii of nums3) {
        for(let iiii of nums4) {
          let temp = i + ii + iii + iiii;
          // console.log(`temp =`, temp);
          if(temp === 0) {
            num += 1;
          }
        }
      }
    }
  }
  // console.log(`num =`, num);
  return num;
};


/*

Runtime Error
<--- Last few GCs --->

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Time Limit Exceeded

[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

[-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,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-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2]


// 测试用例 test cases
const testCases = [
  {
    input: [[1,2], [-2,-1], [-1,2], [0,2]],
    result: 2,
    desc: 'value equal to 2',
  },
  {
    input: [[0], [0], [0], [0]],
    result: 1,
    desc: 'value equal to 1',
  },
  {
    input: [nums, nums, nums, nums],
    result: 100000000,
    desc: 'value equal to 100000000',
  },
  {
    input: [nums12, nums12, nums34, nums34],
    result: 1600000000,
    desc: 'value equal to 1600000000',
  },
];

for (const [i, testCase] of testCases.entries()) {
  console.time(`⏰ performance`);
  const [first, second, third, fourth] = testCase.input;
  const result = fourSumCount(first, second, third, fourth);
  log(`test case ${i} result: `, result === testCase.result ? `✅ passed` : `❌ failed`, result);
  console.timeLog(`⏰ performance`);
  console.timeEnd(`⏰ performance`);
}



// $ npx ts-node ./454\ 4sum-ii.ts

/*

test case 0 result:  ✅ passed 2
⏰ performance: 1.12ms
⏰ performance: 1.268ms
test case 1 result:  ✅ passed 1
⏰ performance: 0.044ms
⏰ performance: 0.079ms
test case 2 result:  ✅ passed 100000000
⏰ performance: 212.757ms
⏰ performance: 212.815ms
test case 3 result:  ✅ passed 1600000000
⏰ performance: 3.090s
⏰ performance: 3.090s


*/



(

标签:console,js,checker,let,result,ms,performance,const
From: https://www.cnblogs.com/xgqfrms/p/17134770.html

相关文章

  • 前端vue的JsPDF html2canvas 生成pdf并以文件流形式上传到后端(转载)
    原文地址1.首先在文件内引入htmlToPdf.js这里代码引入了html2canvas和jspdf//需要npmihtml2Canvas和npmijspdf在这里将getPdf这个函数挂载到Vue的原型上,最后retu......
  • js代码按块编译
    1<!DOCTYPEhtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<title>js代码按块编译</title>6</head>7<body>8910<script>1......
  • jstl 函数
         下面是JSTL中自带的方法列表以及其描述函数名函数说明使用举例fn:contains判断字符串是否包含另外一个字符串<c:iftest="${fn:contains(name,searchString)}">......
  • Fastjson2基础使用以及底层序列化/反序列化实现探究
    1Fastjson2简介Fastjson2是Fastjson的升级版,特征:协议支持:支持JSON/JSONB两种协议部分解析:可以使用JSONPath进行部分解析获取需要的值语言支持:Java/Kotlin场景支持:An......
  • nodejs
     Node.js/usr/local/bin/nodenpm /usr/local/bin/npm Thispackagehasinstalled:Node.jsv19.6.1to/usr/local/bin/nodenpmv9.4.0to/usr/local/bin/npm......
  • JS 删除确认
    if(confirm("您确定要删除吗?")){//dosomething执行删除操作.window.location.href="<%=request.getContextPath()%>/truncatef21v.action";}e......
  • js下载远程图片
    a标签的download属性无法下载远程图片可以将图片转换成blob然后下载如<ahref="https://xxxx.xxx/xxxx.png"download="xxx.png">下载</a>转换blob下载/***blob......
  • egg.js. demo
    丑团git前端expo​​​https://gitee.com/honbingitee/ugly-tuan-expo​​​后端egg.js​​https://gitee.com/honbingitee/ugly-tuan-egg​​/*eslint-disableinde......
  • NodeJS安装(Mac版本)
    nvm,node,npm之间的区别nvm:nodejs版本管理工具。也就是说:一个nvm可以管理很多node版本和npm版本。nodejs:在项目开发时的所需要的代码库npm:nodejs包管理工具。......
  • [State Machine] Zag-js
    import{createMachine}from"@zag-js/core";typeMachineState={value:"idle"|"focused";};typeMachineContext={value:string[];focusedIndex......