UNPKG

6.18 kBMarkdownView Raw
1# kind-of [![NPM version](https://badge.fury.io/js/kind-of.svg)](http://badge.fury.io/js/kind-of) [![Build Status](https://travis-ci.org/jonschlinkert/kind-of.svg)](https://travis-ci.org/jonschlinkert/kind-of)
2
3> Get the native type of a value.
4
5[](#optimizations)**What makes this so fast?**
6
7## Install
8
9Install with [npm](https://www.npmjs.com/)
10
11```sh
12$ npm i kind-of --save
13```
14
15Install with [bower](http://bower.io/)
16
17```sh
18$ bower install kind-of --save
19```
20
21## Usage
22
23> es5, browser and es6 ready
24
25```js
26var kindOf = require('kind-of');
27
28kindOf(undefined);
29//=> 'undefined'
30
31kindOf(null);
32//=> 'null'
33
34kindOf(true);
35//=> 'boolean'
36
37kindOf(false);
38//=> 'boolean'
39
40kindOf(new Boolean(true));
41//=> 'boolean'
42
43kindOf(new Buffer(''));
44//=> 'buffer'
45
46kindOf(42);
47//=> 'number'
48
49kindOf(new Number(42));
50//=> 'number'
51
52kindOf('str');
53//=> 'string'
54
55kindOf(new String('str'));
56//=> 'string'
57
58kindOf(arguments);
59//=> 'arguments'
60
61kindOf({});
62//=> 'object'
63
64kindOf(Object.create(null));
65//=> 'object'
66
67kindOf(new Test());
68//=> 'object'
69
70kindOf(new Date());
71//=> 'date'
72
73kindOf([]);
74//=> 'array'
75
76kindOf([1, 2, 3]);
77//=> 'array'
78
79kindOf(new Array());
80//=> 'array'
81
82kindOf(/[\s\S]+/);
83//=> 'regexp'
84
85kindOf(new RegExp('^' + 'foo$'));
86//=> 'regexp'
87
88kindOf(function () {});
89//=> 'function'
90
91kindOf(function * () {});
92//=> 'function'
93
94kindOf(new Function());
95//=> 'function'
96
97kindOf(new Map());
98//=> 'map'
99
100kindOf(new WeakMap());
101//=> 'weakmap'
102
103kindOf(new Set());
104//=> 'set'
105
106kindOf(new WeakSet());
107//=> 'weakset'
108
109kindOf(Symbol('str'));
110//=> 'symbol'
111```
112
113## Related projects
114
115* [is-number](https://github.com/jonschlinkert/is-number): Returns true if the value is a number. comprehensive tests.
116* [isobject](https://github.com/jonschlinkert/isobject): Returns true if the value is an object and not an array or null.
117* [is-primitive](https://github.com/jonschlinkert/is-primitive): Returns `true` if the value is a primitive.
118* [is-plain-object](https://github.com/jonschlinkert/is-plain-object): Returns true if an object was created by the `Object` constructor.
119* [is-match](https://github.com/jonschlinkert/is-match): Create a matching function from a glob pattern, regex, string, array or function.
120
121## Benchmarks
122
123Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of).
124Note that performaces is slower for es6 features `Map`, `WeakMap`, `Set` and `WeakSet`.
125
126```bash
127#1: array
128 current x 23,329,397 ops/sec ±0.82% (94 runs sampled)
129 lib-type-of x 4,170,273 ops/sec ±0.55% (94 runs sampled)
130 lib-typeof x 9,686,935 ops/sec ±0.59% (98 runs sampled)
131
132#2: boolean
133 current x 27,197,115 ops/sec ±0.85% (94 runs sampled)
134 lib-type-of x 3,145,791 ops/sec ±0.73% (97 runs sampled)
135 lib-typeof x 9,199,562 ops/sec ±0.44% (99 runs sampled)
136
137#3: date
138 current x 20,190,117 ops/sec ±0.86% (92 runs sampled)
139 lib-type-of x 5,166,970 ops/sec ±0.74% (94 runs sampled)
140 lib-typeof x 9,610,821 ops/sec ±0.50% (96 runs sampled)
141
142#4: function
143 current x 23,855,460 ops/sec ±0.60% (97 runs sampled)
144 lib-type-of x 5,667,740 ops/sec ±0.54% (100 runs sampled)
145 lib-typeof x 10,010,644 ops/sec ±0.44% (100 runs sampled)
146
147#5: null
148 current x 27,061,047 ops/sec ±0.97% (96 runs sampled)
149 lib-type-of x 13,965,573 ops/sec ±0.62% (97 runs sampled)
150 lib-typeof x 8,460,194 ops/sec ±0.61% (97 runs sampled)
151
152#6: number
153 current x 25,075,682 ops/sec ±0.53% (99 runs sampled)
154 lib-type-of x 2,266,405 ops/sec ±0.41% (98 runs sampled)
155 lib-typeof x 9,821,481 ops/sec ±0.45% (99 runs sampled)
156
157#7: object
158 current x 3,348,980 ops/sec ±0.49% (99 runs sampled)
159 lib-type-of x 3,245,138 ops/sec ±0.60% (94 runs sampled)
160 lib-typeof x 9,262,952 ops/sec ±0.59% (99 runs sampled)
161
162#8: regex
163 current x 21,284,827 ops/sec ±0.72% (96 runs sampled)
164 lib-type-of x 4,689,241 ops/sec ±0.43% (100 runs sampled)
165 lib-typeof x 8,957,593 ops/sec ±0.62% (98 runs sampled)
166
167#9: string
168 current x 25,379,234 ops/sec ±0.58% (96 runs sampled)
169 lib-type-of x 3,635,148 ops/sec ±0.76% (93 runs sampled)
170 lib-typeof x 9,494,134 ops/sec ±0.49% (98 runs sampled)
171
172#10: undef
173 current x 27,459,221 ops/sec ±1.01% (93 runs sampled)
174 lib-type-of x 14,360,433 ops/sec ±0.52% (99 runs sampled)
175 lib-typeof x 23,202,868 ops/sec ±0.59% (94 runs sampled)
176```
177
178## Optimizations
179
180In 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:
181
1821. 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.
1832. 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.
1843. 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'`
185
186## Running tests
187
188Install dev dependencies:
189
190```sh
191$ npm i -d && npm test
192```
193
194## Contributing
195
196Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/kind-of/issues/new)
197
198## Author
199
200**Jon Schlinkert**
201
202+ [github/jonschlinkert](https://github.com/jonschlinkert)
203+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
204
205## License
206
207Copyright © 2014-2015 [Jon Schlinkert](https://github.com/jonschlinkert)
208Released under the MIT license.
209
210***
211
212_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 31, 2015._