dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

refactor: optimize isToday, isTomorrow and isYesterday performance

Open BiosSun opened this issue 4 years ago • 2 comments

The performance is improved by 6 times:

new x 301,874 ops/sec ±0.87% (96 runs sampled)
old x 48,029 ops/sec ±0.61% (94 runs sampled)
const Benchmark = require('benchmark')

const newDayjs = require('./dayjs.min')
newDayjs.extend(require('./plugin/isToday'))
newDayjs.extend(require('./plugin/isTomorrow'))
newDayjs.extend(require('./plugin/isYesterday'))

const oldDayjs = require('dayjs')
oldDayjs.extend(require('dayjs/plugin/isToday'))
oldDayjs.extend(require('dayjs/plugin/isTomorrow'))
oldDayjs.extend(require('dayjs/plugin/isYesterday'))

const newToday = newDayjs()
const newTomorrow = newDayjs().add(1, 'day')
const newYesterday = newDayjs().subtract(1, 'day')

const oldToday = oldDayjs()
const oldTomorrow = oldDayjs().add(1, 'day')
const oldYesterday = oldDayjs().subtract(1, 'day')

const suite = new Benchmark.Suite()

suite
  .add('new', () => {
    newToday.isToday()
    newTomorrow.isTomorrow()
    newYesterday.isYesterday()
  })
  .add('old', () => {
    oldToday.isToday()
    oldTomorrow.isTomorrow()
    oldYesterday.isYesterday()
  })
  .on('cycle', (event) => {
    console.log(String(event.target))
  })
  .run({ async: true })

BiosSun avatar Sep 07 '21 01:09 BiosSun

Codecov Report

Merging #1628 (80151c6) into dev (f68e4b1) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##               dev     #1628   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          177       177           
  Lines         1989      1995    +6     
  Branches       505       512    +7     
=========================================
+ Hits          1989      1995    +6     
Impacted Files Coverage Δ
src/plugin/isToday/index.js 100.00% <100.00%> (ø)
src/plugin/isTomorrow/index.js 100.00% <100.00%> (ø)
src/plugin/isYesterday/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f68e4b1...80151c6. Read the comment docs.

codecov[bot] avatar Sep 07 '21 01:09 codecov[bot]

amazing!

iamkun avatar Sep 10 '21 09:09 iamkun