jonschlinkert / kind-of

Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
https://github.com/jonschlnkert
MIT License
347 stars 38 forks source link

I wrote one , need your evaluation. #38

Open yinsang opened 3 years ago

yinsang commented 3 years ago
function priviteKindOf(sth) {
    if (typeof sth === 'object' || typeof sth === 'function') {
        const type = /\[object (.*)\]/.exec(Object.prototype.toString.call(sth))[1].toLowerCase().replace(/\s/, '')

        if (type === 'uint8array' && sth instanceof Buffer) {
            return 'buffer'
        }
        return type
    }
    return typeof sth
}
module.exports = priviteKindOf

Passed all your tests, simpler, but performance is not so good. Benchmarking: (21 of 21), priviteKindOf's better amount is 6, kindOf is 15.

# benchmark/fixtures/arguments.js (32 bytes)
  kind-of x 46,303,737 ops/sec ±0.84% (87 runs sampled)
  priviteKindOf x 49,144,123 ops/sec ±0.44% (93 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/array.js (22 bytes)
  kind-of x 35,050,057 ops/sec ±0.55% (94 runs sampled)
  priviteKindOf x 5,403,902 ops/sec ±1.34% (89 runs sampled)

  fastest is kind-of

# benchmark/fixtures/boolean.js (24 bytes)
  kind-of x 39,480,494 ops/sec ±0.81% (93 runs sampled)
  priviteKindOf x 50,521,049 ops/sec ±0.63% (91 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/buffer.js (38 bytes)
  kind-of x 26,790,947 ops/sec ±0.64% (93 runs sampled)
  priviteKindOf x 1,499,540 ops/sec ±0.55% (92 runs sampled)

  fastest is kind-of

# benchmark/fixtures/date.js (30 bytes)
  kind-of x 21,006,428 ops/sec ±1.22% (87 runs sampled)
  priviteKindOf x 5,258,425 ops/sec ±0.40% (93 runs sampled)

  fastest is kind-of

# benchmark/fixtures/error.js (36 bytes)
  kind-of x 17,555,723 ops/sec ±1.21% (89 runs sampled)
  priviteKindOf x 5,578,384 ops/sec ±0.32% (96 runs sampled)

  fastest is kind-of

# benchmark/fixtures/function.js (34 bytes)
  kind-of x 18,311,781 ops/sec ±0.28% (91 runs sampled)
  priviteKindOf x 5,278,844 ops/sec ±0.44% (92 runs sampled)

  fastest is kind-of

# benchmark/fixtures/generator.js (39 bytes)
  kind-of x 16,307,369 ops/sec ±0.82% (90 runs sampled)
  priviteKindOf x 1,493,459 ops/sec ±0.71% (91 runs sampled)

  fastest is kind-of

# benchmark/fixtures/map.js (30 bytes)
  kind-of x 8,123,220 ops/sec ±0.79% (90 runs sampled)
  priviteKindOf x 4,127,706 ops/sec ±0.33% (92 runs sampled)

  fastest is kind-of

# benchmark/fixtures/null.js (24 bytes)
  kind-of x 36,778,380 ops/sec ±3.99% (79 runs sampled)
  priviteKindOf x 5,392,973 ops/sec ±0.54% (91 runs sampled)

  fastest is kind-of

# benchmark/fixtures/number.js (22 bytes)
  kind-of x 37,574,334 ops/sec ±3.44% (84 runs sampled)
  priviteKindOf x 33,370,196 ops/sec ±1.52% (87 runs sampled)

  fastest is kind-of

# benchmark/fixtures/object-instance.js (22 bytes)
  kind-of x 6,790,378 ops/sec ±0.62% (91 runs sampled)
  priviteKindOf x 5,270,510 ops/sec ±0.63% (92 runs sampled)

  fastest is kind-of

# benchmark/fixtures/object-plain.js (47 bytes)
  kind-of x 5,928,275 ops/sec ±0.55% (91 runs sampled)
  priviteKindOf x 5,085,198 ops/sec ±0.58% (91 runs sampled)

  fastest is kind-of

# benchmark/fixtures/regex.js (25 bytes)
  kind-of x 11,512,111 ops/sec ±1.38% (83 runs sampled)
  priviteKindOf x 5,081,246 ops/sec ±0.64% (91 runs sampled)

  fastest is kind-of

# benchmark/fixtures/set.js (30 bytes)
  kind-of x 7,139,086 ops/sec ±0.56% (92 runs sampled)
  priviteKindOf x 3,937,407 ops/sec ±0.48% (91 runs sampled)

  fastest is kind-of

# benchmark/fixtures/string.js (33 bytes)
  kind-of x 26,591,874 ops/sec ±1.28% (89 runs sampled)
  priviteKindOf x 32,886,354 ops/sec ±1.59% (88 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/symbol.js (34 bytes)
  kind-of x 27,769,816 ops/sec ±1.37% (88 runs sampled)
  priviteKindOf x 34,443,818 ops/sec ±2.29% (69 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/template-strings.js (36 bytes)
  kind-of x 26,634,815 ops/sec ±1.15% (89 runs sampled)
  priviteKindOf x 33,198,302 ops/sec ±1.55% (87 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/undefined.js (29 bytes)
  kind-of x 31,228,852 ops/sec ±1.62% (70 runs sampled)
  priviteKindOf x 34,936,430 ops/sec ±1.83% (83 runs sampled)

  fastest is priviteKindOf

# benchmark/fixtures/weakmap.js (34 bytes)
  kind-of x 6,802,391 ops/sec ±0.55% (92 runs sampled)
  priviteKindOf x 1,791,358 ops/sec ±0.87% (92 runs sampled)

  fastest is kind-of

# benchmark/fixtures/weakset.js (34 bytes)
  kind-of x 6,540,679 ops/sec ±0.72% (90 runs sampled)
  priviteKindOf x 1,722,102 ops/sec ±1.68% (85 runs sampled)

  fastest is kind-of