Berkmann18 / NativeVsUtils

Native functions/methods vs util methods in JS
9 stars 3 forks source link

Bug in nativeVsLodash.js when benchmarking _.replace #3

Open victor-homyakov opened 3 years ago

victor-homyakov commented 3 years ago

Expected behaviour

Benchmark for replace should compare operations on the same string txt

replaceSuite
  .add('lodash', () => _.replace(txt, re, 'oranges'))
  .add('native', () => txt.replace(re, 'oranges'))

Actual behaviour

_.replace tries to make a replacement on non-existent string str and silently fails. Therefore there is no actual comparison for replace.

replaceSuite
  .add('lodash', () => _.replace(str, re, 'oranges'))
  .add('native', () => txt.replace(re, 'oranges'))

How to reproduce

  1. node nativeVsLodash.js
  2. Note that there is no speed output for lodash, and native is marked both as the fastest and the slowest:
    Benchmark: replace
    The fastest is native
    The slowest is native

Possible solution(s)

Fix the code:

replaceSuite
  .add('lodash', () => _.replace(txt, re, 'oranges'))
  .add('native', () => txt.replace(re, 'oranges'))

Also, the RegExp in this suite is global - this may affect results.

Environment

Link or repo

https://github.com/Berkmann18/NativeVsUtils/blob/master/nativeVsLodash.js#L395-L396

issue-label-bot[bot] commented 3 years ago

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.98. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.