Code Monkey home page Code Monkey logo

time-statistics's Introduction

作用

用于时间埋点,计算统计耗时

底层用一个 Map 来存储所有被记录的时刻,每个被记录的时刻有唯一的名称即markName

MapkeymarkNamevalue 为自1970年1月1日 00:00:00 UTC到被记录时刻的毫秒数。

使用示例

 import timeStatistics from 'time-statistics';   //引入

 timeStatistics.mark('firstFocus');  //记录输入框第一次聚焦的时刻

 timeStatistics.mark('firstBlur');  //记录输入框第一次失焦的时刻

 let _time = timeStatistics.measure('firstFocus'); //记算耗时:从第一次聚焦时刻至当前时刻的时间间隔

 let _time = timeStatistics.measure('firstFocus', 'firstBlur'); //记算耗时:从第一次聚焦至第一次失焦的时间间隔

 timeStatistics.clearMarks('firstFocus');  //清空第一次聚焦的时刻

 timeStatistics.clearMarks();  //清空所有记录的时刻
 

API 说明

一共提供了3个接口:markmeasureclearMarks

所有接口的设计参考了 user performance 规范

  1. statisticTiming.mark(markName)

    作用:标记函数,用来记录当前时刻

    参数:

    1. markName
      • 必填
      • 当前时刻的标记名称
      • 类型:string

    返回值:

    • 自1970年1月1日 00:00:00 UTC到当前时间的毫秒数

    注意: 当多次调用mark方法时传入同一个markname值,只会记录最后一次调用的时刻

  2. statisticTiming.measure(startMark, endMark)

    作用:计算耗时函数,计算起始标记时刻之间的时间间隔

    参数:

    1. startMark
     - 必填
     - 开始时刻的 `markName`
     - 类型:`string`
    
    1. endMark
      • 选填
      • 结束时刻的 markName
      • 类型:string

    返回值:

    • startMarkendMark 都存在时,则返回 endMarkstartMark 的时间间隔(毫秒数);
    • 若仅 startMark 存在,而 endMark 不存在,则返回当前时刻与 startMark 的时间间隔(毫秒数);
  3. statisticTiming.clearMarks(markName)

    作用:清空函数,若markName存在,则清空某个标记时刻; 若markName不存在,则清空所有标记时刻

    参数:

    1. markName
      • 选填
      • 要清空时刻的标记名称
      • 类型:string

    返回值:

    • undefined

time-statistics's People

Watchers

James Cloos avatar ruoyiqing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.