jorgebucaran / classcat

Build a class attribute string quickly
MIT License
906 stars 22 forks source link

Benchmark is kinda unfair #33

Closed vladshcherbin closed 4 years ago

vladshcherbin commented 4 years ago

Hey 👋

I've check provided string benchmark which has ['one', 'two', 'three'] argument. It produces one two three class.

Both classnames and clsx don't need to use array argument for this - clsx('one', 'two', 'three') will produce same class.

Would be nice to have this reflected in benchmark to see if it has any effect. 🤔

vladshcherbin commented 4 years ago

I've quickly checked a simple benchmark:

const suite = new Suite()

suite
  .add('classcat', function() {
    classcat(['one', 'two', 'three'])
  })
  .add('classnames', function() {
    classnames('one', 'two', 'three')
  })
  .add('clsx', function() {
    clsx('one', 'two', 'three')
  })
  .on('cycle', function({ target: { name, hz } }) {
    console.log(`${name} × ${Math.floor(hz).toLocaleString()} ops/sec`)
  })
  .run()

Results are:

classcat × 10,762,320 ops/sec
classnames × 4,166,352 ops/sec
clsx × 11,792,268 ops/sec

Using array of strings in same test give:

classcat × 10,407,374 ops/sec
classnames × 2,384,962 ops/sec
clsx × 8,506,937 ops/sec

It's kinda big perf drop 🤔

jorgebucaran commented 4 years ago

@vladshcherbin I have benchmark tests for all usage styles, except var args because classcat doesn't allow this usage (I don't like arguments) and it was kinda difficult to add a unique test case for it using my stubbornly declarative approach, but you are right, so I've gone out my way to add add a unique test only for these libraries.

I've also cut a new release of classcat where it's still faster (just a bit) even against variable arguments. I'm also planning on getting rid of the arguably useless hasOwnProperty guard check in the future.

# Strings
classcat × 15,927,163 ops/sec
classnames × 2,694,533 ops/sec
clsx × 8,542,847 ops/sec

# Objects
classcat × 15,205,051 ops/sec
classnames × 2,873,497 ops/sec
clsx × 8,806,231 ops/sec

# Strings/Objects
classcat × 13,834,475 ops/sec
classnames × 3,013,424 ops/sec
clsx × 5,890,821 ops/sec

# Arrays
classcat × 3,649,723 ops/sec
classnames × 709,177 ops/sec
clsx × 2,513,014 ops/sec

# Arrays/Objects
classcat × 4,290,009 ops/sec
classnames × 1,856,967 ops/sec
clsx × 3,099,573 ops/sec

# Arguments vs Array
classcat × 3,089,353 ops/sec
classnames × 828,906 ops/sec
clsx × 3,057,879 ops/sec
vladshcherbin commented 4 years ago

@jorgebucaran that's awesome, thank you ❤️