What is kind-of?
The kind-of npm package is a utility library that allows developers to check the type of a given value in JavaScript. It provides a simple API to determine whether a value is a string, number, array, object, function, and other JavaScript types. This can be particularly useful for type checking and validation in applications where type safety is important.
What are kind-of's main functionalities?
Type checking for primitives
This feature allows you to check the type of primitive values such as numbers, strings, and booleans.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(1)); // 'number' console.log(kindOf('hello')); // 'string' console.log(kindOf(true)); // 'boolean'
Type checking for objects
This feature allows you to check the type of object-based values such as plain objects, arrays, and regular expressions.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf({})); // 'object' console.log(kindOf([])); // 'array' console.log(kindOf(/foo/)); // 'regexp'
Type checking for functions and undefined
This feature allows you to check the type of functions and the undefined value.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(function () {})); // 'function' console.log(kindOf(undefined)); // 'undefined'
Type checking for null and dates
This feature allows you to check the type of null values and date objects.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(null)); // 'null' console.log(kindOf(new Date())); // 'date'
Other packages similar to kind-of
type-check
The 'type-check' package offers a similar functionality to 'kind-of' by allowing type assertions and checks. It provides a mini language to specify the types of values and can validate against those specifications.
is
The 'is' package is another utility library that provides type checking functions. It has a wider range of type checks available, including checks for specific JavaScript environments like the browser or Node.js.
check-types
The 'check-types' package provides a set of predicates for type checking. It has a fluent API and includes additional checks for min/max values, empty strings, and arrays, which are not directly available in 'kind-of'.
kind-of
Get the native type of a value.
Install
Install with npm:
$ npm install --save kind-of
Install
Install with bower
$ bower install kind-of --save
Usage
es5, browser and es6 ready
var kindOf = require('kind-of');
kindOf(undefined);
kindOf(null);
kindOf(true);
kindOf(false);
kindOf(new Boolean(true));
kindOf(new Buffer(''));
kindOf(42);
kindOf(new Number(42));
kindOf('str');
kindOf(new String('str'));
kindOf(arguments);
kindOf({});
kindOf(Object.create(null));
kindOf(new Test());
kindOf(new Date());
kindOf([]);
kindOf([1, 2, 3]);
kindOf(new Array());
kindOf(/foo/);
kindOf(new RegExp('foo'));
kindOf(function () {});
kindOf(function * () {});
kindOf(new Function());
kindOf(new Map());
kindOf(new WeakMap());
kindOf(new Set());
kindOf(new WeakSet());
kindOf(Symbol('str'));
kindOf(new Int8Array());
kindOf(new Uint8Array());
kindOf(new Uint8ClampedArray());
kindOf(new Int16Array());
kindOf(new Uint16Array());
kindOf(new Int32Array());
kindOf(new Uint32Array());
kindOf(new Float32Array());
kindOf(new Float64Array());
Benchmarks
Benchmarked against typeof and type-of.
Note that performaces is slower for es6 features Map
, WeakMap
, Set
and WeakSet
.
current x 23,329,397 ops/sec ±0.82% (94 runs sampled)
lib-type-of x 4,170,273 ops/sec ±0.55% (94 runs sampled)
lib-typeof x 9,686,935 ops/sec ±0.59% (98 runs sampled)
current x 27,197,115 ops/sec ±0.85% (94 runs sampled)
lib-type-of x 3,145,791 ops/sec ±0.73% (97 runs sampled)
lib-typeof x 9,199,562 ops/sec ±0.44% (99 runs sampled)
current x 20,190,117 ops/sec ±0.86% (92 runs sampled)
lib-type-of x 5,166,970 ops/sec ±0.74% (94 runs sampled)
lib-typeof x 9,610,821 ops/sec ±0.50% (96 runs sampled)
current x 23,855,460 ops/sec ±0.60% (97 runs sampled)
lib-type-of x 5,667,740 ops/sec ±0.54% (100 runs sampled)
lib-typeof x 10,010,644 ops/sec ±0.44% (100 runs sampled)
current x 27,061,047 ops/sec ±0.97% (96 runs sampled)
lib-type-of x 13,965,573 ops/sec ±0.62% (97 runs sampled)
lib-typeof x 8,460,194 ops/sec ±0.61% (97 runs sampled)
current x 25,075,682 ops/sec ±0.53% (99 runs sampled)
lib-type-of x 2,266,405 ops/sec ±0.41% (98 runs sampled)
lib-typeof x 9,821,481 ops/sec ±0.45% (99 runs sampled)
current x 3,348,980 ops/sec ±0.49% (99 runs sampled)
lib-type-of x 3,245,138 ops/sec ±0.60% (94 runs sampled)
lib-typeof x 9,262,952 ops/sec ±0.59% (99 runs sampled)
current x 21,284,827 ops/sec ±0.72% (96 runs sampled)
lib-type-of x 4,689,241 ops/sec ±0.43% (100 runs sampled)
lib-typeof x 8,957,593 ops/sec ±0.62% (98 runs sampled)
current x 25,379,234 ops/sec ±0.58% (96 runs sampled)
lib-type-of x 3,635,148 ops/sec ±0.76% (93 runs sampled)
lib-typeof x 9,494,134 ops/sec ±0.49% (98 runs sampled)
current x 27,459,221 ops/sec ±1.01% (93 runs sampled)
lib-type-of x 14,360,433 ops/sec ±0.52% (99 runs sampled)
lib-typeof x 23,202,868 ops/sec ±0.59% (94 runs sampled)
Optimizations
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 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'
About
Related projects
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert.
Released under the MIT license.
This file was generated by verb-generate-readme, v0.2.0, on December 07, 2016.