Thanks to the following companies, organizations, and individuals for supporting the ongoing maintenance and development of {%= name %}! Become a Sponsor to add your logo to this README, or any of my other projects
https://jaake.tech/ |
es5, es6, and browser ready
const kindOf = require('{%= name %}');
kindOf(undefined);
//=> 'undefined'
kindOf(null);
//=> 'null'
kindOf(true);
//=> 'boolean'
kindOf(false);
//=> 'boolean'
kindOf(new Buffer(''));
//=> 'buffer'
kindOf(42);
//=> 'number'
kindOf('str');
//=> 'string'
kindOf(arguments);
//=> 'arguments'
kindOf({});
//=> 'object'
kindOf(Object.create(null));
//=> 'object'
kindOf(new Test());
//=> 'object'
kindOf(new Date());
//=> 'date'
kindOf([1, 2, 3]);
//=> 'array'
kindOf(/foo/);
//=> 'regexp'
kindOf(new RegExp('foo'));
//=> 'regexp'
kindOf(new Error('error'));
//=> 'error'
kindOf(function () {});
//=> 'function'
kindOf(function * () {});
//=> 'generatorfunction'
kindOf(Symbol('str'));
//=> 'symbol'
kindOf(new Map());
//=> 'map'
kindOf(new WeakMap());
//=> 'weakmap'
kindOf(new Set());
//=> 'set'
kindOf(new WeakSet());
//=> 'weakset'
kindOf(new Int8Array());
//=> 'int8array'
kindOf(new Uint8Array());
//=> 'uint8array'
kindOf(new Uint8ClampedArray());
//=> 'uint8clampedarray'
kindOf(new Int16Array());
//=> 'int16array'
kindOf(new Uint16Array());
//=> 'uint16array'
kindOf(new Int32Array());
//=> 'int32array'
kindOf(new Uint32Array());
//=> 'uint32array'
kindOf(new Float32Array());
//=> 'float32array'
kindOf(new Float64Array());
//=> 'float64array'
Benchmarked against typeof and type-of.
{%= include("benchmark/stats.md") %}
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
typeof
checks 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
Object
constructor). I opted to make this check happen by process of elimination rather than brute force up front (e.g. by using something likeval.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 wordregex
? It's much faster to doif (type === '[object RegExp]') return 'regex'
- There is no reason to make the code in a microlib as terse as possible, just to win points for making it shorter. It's always better to favor performant code over terse code. You will always only be using a single
require()
statement to use the library anyway, regardless of how the code is written.
kind-of seems to be more consistently "correct" than other type checking libs I've looked at. For example, here are some differing results from other popular libs:
Incorrectly identifies instances of custom constructors (pretty common):
var typeOf = require('typeof');
function Test() {}
console.log(typeOf(new Test()));
//=> 'test'
Returns object
instead of arguments
:
function foo() {
console.log(typeOf(arguments)) //=> 'object'
}
foo();
Incorrectly returns object
for generator functions, buffers, Map
, Set
, WeakMap
and WeakSet
:
function * foo() {}
console.log(typeOf(foo));
//=> 'object'
console.log(typeOf(new Buffer('')));
//=> 'object'
console.log(typeOf(new Map()));
//=> 'object'
console.log(typeOf(new Set()));
//=> 'object'
console.log(typeOf(new WeakMap()));
//=> 'object'
console.log(typeOf(new WeakSet()));
//=> 'object'