Get the native type of a value.
Install with npm
$ npm i kind-of --save
Install with bower
$ bower install kind-of --save
es5, browser and es6 ready
var kindOf = ;;//=> 'undefined';//=> 'null';//=> 'boolean';//=> 'boolean';//=> 'boolean';//=> 'buffer';//=> 'number';//=> 'number';//=> 'string';//=> 'string';//=> 'arguments';//=> 'object';//=> 'object';//=> 'object';//=> 'date';//=> 'array';//=> 'array';//=> 'array';//=> 'regexp';//=> 'regexp';//=> 'function';//=> 'function';//=> 'function';//=> 'map';//=> 'weakmap';//=> 'set';//=> 'weakset';//=> 'symbol'
- is-number: Returns true if the value is a number. comprehensive tests.
- isobject: Returns true if the value is an object and not an array or null.
- is-primitive: Returns
trueif the value is a primitive.
- is-plain-object: Returns true if an object was created by the
- is-match: Create a matching function from a glob pattern, regex, string, array or function.
#1: arraycurrent x 23,329,397 ops/sec ±0.82%lib-type-of x 4,170,273 ops/sec ±0.55%lib-typeof x 9,686,935 ops/sec ±0.59%#2: booleancurrent x 27,197,115 ops/sec ±0.85%lib-type-of x 3,145,791 ops/sec ±0.73%lib-typeof x 9,199,562 ops/sec ±0.44%#3: datecurrent x 20,190,117 ops/sec ±0.86%lib-type-of x 5,166,970 ops/sec ±0.74%lib-typeof x 9,610,821 ops/sec ±0.50%#4: functioncurrent x 23,855,460 ops/sec ±0.60%lib-type-of x 5,667,740 ops/sec ±0.54%lib-typeof x 10,010,644 ops/sec ±0.44%#5: nullcurrent x 27,061,047 ops/sec ±0.97%lib-type-of x 13,965,573 ops/sec ±0.62%lib-typeof x 8,460,194 ops/sec ±0.61%#6: numbercurrent x 25,075,682 ops/sec ±0.53%lib-type-of x 2,266,405 ops/sec ±0.41%lib-typeof x 9,821,481 ops/sec ±0.45%#7: objectcurrent x 3,348,980 ops/sec ±0.49%lib-type-of x 3,245,138 ops/sec ±0.60%lib-typeof x 9,262,952 ops/sec ±0.59%#8: regexcurrent x 21,284,827 ops/sec ±0.72%lib-type-of x 4,689,241 ops/sec ±0.43%lib-typeof x 8,957,593 ops/sec ±0.62%#9: stringcurrent x 25,379,234 ops/sec ±0.58%lib-type-of x 3,635,148 ops/sec ±0.76%lib-typeof x 9,494,134 ops/sec ±0.49%#10: undefcurrent x 27,459,221 ops/sec ±1.01%lib-type-of x 14,360,433 ops/sec ±0.52%lib-typeof x 23,202,868 ops/sec ±0.59%
In 7 out of 8 cases, this library is 2x-10x faster than other top libraries included in the benchmarks. There are a few things that lead to this performance advantage, none of them hard and fast rules, but all of them simple and repeatable in almost any code library:
- Optimize around the fastest and most common use cases first. Of course, this will change from project-to-project, but I took some time to understand how and why
typeofchecks were being used in my own libraries and other libraries I use a lot.
- Optimize around bottlenecks - In other words, the order in which conditionals are implemented is significant, because each check is only as fast as the failing checks that came before it. Here, the biggest bottleneck by far is checking for plain objects (an object that was created by the
Objectconstructor). I opted to make this check happen by process of elimination rather than brute force up front (e.g. by using something like
val.constructor.name), so that every other type check would not be penalized it.
- Don't do uneccessary processing - why do
.slice(8, -1).toLowerCase();just to get the word
regex? It's much faster to do
if (type === '[object RegExp]') return 'regex'
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Copyright © 2014-2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb-cli on May 31, 2015.