README.md
  1  # kind-of [![NPM version](https://img.shields.io/npm/v/kind-of.svg?style=flat)](https://www.npmjs.com/package/kind-of) [![NPM monthly downloads](https://img.shields.io/npm/dm/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![NPM total downloads](https://img.shields.io/npm/dt/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/kind-of.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/kind-of)
  2  
  3  > Get the native type of a value.
  4  
  5  Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  6  
  7  ## Install
  8  
  9  Install with [npm](https://www.npmjs.com/):
 10  
 11  ```sh
 12  $ npm install --save kind-of
 13  ```
 14  
 15  Install with [bower](https://bower.io/)
 16  
 17  ```sh
 18  $ bower install kind-of --save
 19  ```
 20  
 21  ## Why use this?
 22  
 23  1. [it's fast](#benchmarks) | [optimizations](#optimizations)
 24  2. [better type checking](#better-type-checking)
 25  
 26  ## Usage
 27  
 28  > es5, browser and es6 ready
 29  
 30  ```js
 31  var kindOf = require('kind-of');
 32  
 33  kindOf(undefined);
 34  //=> 'undefined'
 35  
 36  kindOf(null);
 37  //=> 'null'
 38  
 39  kindOf(true);
 40  //=> 'boolean'
 41  
 42  kindOf(false);
 43  //=> 'boolean'
 44  
 45  kindOf(new Boolean(true));
 46  //=> 'boolean'
 47  
 48  kindOf(new Buffer(''));
 49  //=> 'buffer'
 50  
 51  kindOf(42);
 52  //=> 'number'
 53  
 54  kindOf(new Number(42));
 55  //=> 'number'
 56  
 57  kindOf('str');
 58  //=> 'string'
 59  
 60  kindOf(new String('str'));
 61  //=> 'string'
 62  
 63  kindOf(arguments);
 64  //=> 'arguments'
 65  
 66  kindOf({});
 67  //=> 'object'
 68  
 69  kindOf(Object.create(null));
 70  //=> 'object'
 71  
 72  kindOf(new Test());
 73  //=> 'object'
 74  
 75  kindOf(new Date());
 76  //=> 'date'
 77  
 78  kindOf([]);
 79  //=> 'array'
 80  
 81  kindOf([1, 2, 3]);
 82  //=> 'array'
 83  
 84  kindOf(new Array());
 85  //=> 'array'
 86  
 87  kindOf(/foo/);
 88  //=> 'regexp'
 89  
 90  kindOf(new RegExp('foo'));
 91  //=> 'regexp'
 92  
 93  kindOf(function () {});
 94  //=> 'function'
 95  
 96  kindOf(function * () {});
 97  //=> 'function'
 98  
 99  kindOf(new Function());
100  //=> 'function'
101  
102  kindOf(new Map());
103  //=> 'map'
104  
105  kindOf(new WeakMap());
106  //=> 'weakmap'
107  
108  kindOf(new Set());
109  //=> 'set'
110  
111  kindOf(new WeakSet());
112  //=> 'weakset'
113  
114  kindOf(Symbol('str'));
115  //=> 'symbol'
116  
117  kindOf(new Int8Array());
118  //=> 'int8array'
119  
120  kindOf(new Uint8Array());
121  //=> 'uint8array'
122  
123  kindOf(new Uint8ClampedArray());
124  //=> 'uint8clampedarray'
125  
126  kindOf(new Int16Array());
127  //=> 'int16array'
128  
129  kindOf(new Uint16Array());
130  //=> 'uint16array'
131  
132  kindOf(new Int32Array());
133  //=> 'int32array'
134  
135  kindOf(new Uint32Array());
136  //=> 'uint32array'
137  
138  kindOf(new Float32Array());
139  //=> 'float32array'
140  
141  kindOf(new Float64Array());
142  //=> 'float64array'
143  ```
144  
145  ## Release history
146  
147  ### v4.0.0
148  
149  **Added**
150  
151  * `promise` support
152  
153  ### v5.0.0
154  
155  **Added**
156  
157  * `Set Iterator` and `Map Iterator` support
158  
159  **Fixed**
160  
161  * Now returns `generatorfunction` for generator functions
162  
163  ## Benchmarks
164  
165  Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of).
166  Note that performaces is slower for es6 features `Map`, `WeakMap`, `Set` and `WeakSet`.
167  
168  ```bash
169  #1: array
170    current x 23,329,397 ops/sec ±0.82% (94 runs sampled)
171    lib-type-of x 4,170,273 ops/sec ±0.55% (94 runs sampled)
172    lib-typeof x 9,686,935 ops/sec ±0.59% (98 runs sampled)
173  
174  #2: boolean
175    current x 27,197,115 ops/sec ±0.85% (94 runs sampled)
176    lib-type-of x 3,145,791 ops/sec ±0.73% (97 runs sampled)
177    lib-typeof x 9,199,562 ops/sec ±0.44% (99 runs sampled)
178  
179  #3: date
180    current x 20,190,117 ops/sec ±0.86% (92 runs sampled)
181    lib-type-of x 5,166,970 ops/sec ±0.74% (94 runs sampled)
182    lib-typeof x 9,610,821 ops/sec ±0.50% (96 runs sampled)
183  
184  #4: function
185    current x 23,855,460 ops/sec ±0.60% (97 runs sampled)
186    lib-type-of x 5,667,740 ops/sec ±0.54% (100 runs sampled)
187    lib-typeof x 10,010,644 ops/sec ±0.44% (100 runs sampled)
188  
189  #5: null
190    current x 27,061,047 ops/sec ±0.97% (96 runs sampled)
191    lib-type-of x 13,965,573 ops/sec ±0.62% (97 runs sampled)
192    lib-typeof x 8,460,194 ops/sec ±0.61% (97 runs sampled)
193  
194  #6: number
195    current x 25,075,682 ops/sec ±0.53% (99 runs sampled)
196    lib-type-of x 2,266,405 ops/sec ±0.41% (98 runs sampled)
197    lib-typeof x 9,821,481 ops/sec ±0.45% (99 runs sampled)
198  
199  #7: object
200    current x 3,348,980 ops/sec ±0.49% (99 runs sampled)
201    lib-type-of x 3,245,138 ops/sec ±0.60% (94 runs sampled)
202    lib-typeof x 9,262,952 ops/sec ±0.59% (99 runs sampled)
203  
204  #8: regex
205    current x 21,284,827 ops/sec ±0.72% (96 runs sampled)
206    lib-type-of x 4,689,241 ops/sec ±0.43% (100 runs sampled)
207    lib-typeof x 8,957,593 ops/sec ±0.62% (98 runs sampled)
208  
209  #9: string
210    current x 25,379,234 ops/sec ±0.58% (96 runs sampled)
211    lib-type-of x 3,635,148 ops/sec ±0.76% (93 runs sampled)
212    lib-typeof x 9,494,134 ops/sec ±0.49% (98 runs sampled)
213  
214  #10: undef
215    current x 27,459,221 ops/sec ±1.01% (93 runs sampled)
216    lib-type-of x 14,360,433 ops/sec ±0.52% (99 runs sampled)
217    lib-typeof x 23,202,868 ops/sec ±0.59% (94 runs sampled)
218  
219  ```
220  
221  ## Optimizations
222  
223  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:
224  
225  1. 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.
226  2. 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.
227  3. 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'`
228  4. 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.
229  
230  ## Better type checking
231  
232  kind-of is more correct than other type checking libs I've looked at. For example, here are some differing results from other popular libs:
233  
234  ### [typeof](https://github.com/CodingFu/typeof) lib
235  
236  Incorrectly tests instances of custom constructors (pretty common):
237  
238  ```js
239  var typeOf = require('typeof');
240  function Test() {}
241  console.log(typeOf(new Test()));
242  //=> 'test'
243  ```
244  
245  Returns `object` instead of `arguments`:
246  
247  ```js
248  function foo() {
249    console.log(typeOf(arguments)) //=> 'object'
250  }
251  foo();
252  ```
253  
254  ### [type-of](https://github.com/ForbesLindesay/type-of) lib
255  
256  Incorrectly returns `object` for generator functions, buffers, `Map`, `Set`, `WeakMap` and `WeakSet`:
257  
258  ```js
259  function * foo() {}
260  console.log(typeOf(foo));
261  //=> 'object'
262  console.log(typeOf(new Buffer('')));
263  //=> 'object'
264  console.log(typeOf(new Map()));
265  //=> 'object'
266  console.log(typeOf(new Set()));
267  //=> 'object'
268  console.log(typeOf(new WeakMap()));
269  //=> 'object'
270  console.log(typeOf(new WeakSet()));
271  //=> 'object'
272  ```
273  
274  ## About
275  
276  <details>
277  <summary><strong>Contributing</strong></summary>
278  
279  Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
280  
281  <details>
282  
283  <details>
284  <summary><strong>Running Tests</strong></summary>
285  
286  Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
287  
288  ```sh
289  $ npm install && npm test
290  ```
291  
292  <details>
293  
294  <details>
295  <summary><strong>Building docs</strong></summary>
296  
297  _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
298  
299  To generate the readme, run the following command:
300  
301  ```sh
302  $ npm install -g verbose/verb#dev verb-generate-readme && verb
303  ```
304  
305  <details>
306  
307  ### Related projects
308  
309  You might also be interested in these projects:
310  
311  * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
312  * [is-number](https://www.npmjs.com/package/is-number): Returns true if the value is a number. comprehensive tests. | [homepage](https://github.com/jonschlinkert/is-number "Returns true if the value is a number. comprehensive tests.")
313  * [is-primitive](https://www.npmjs.com/package/is-primitive): Returns `true` if the value is a primitive.  | [homepage](https://github.com/jonschlinkert/is-primitive "Returns `true` if the value is a primitive. ")
314  
315  ### Contributors
316  
317  | **Commits** | **Contributor** | 
318  | --- | --- |
319  | 82 | [jonschlinkert](https://github.com/jonschlinkert) |
320  | 3 | [aretecode](https://github.com/aretecode) |
321  | 2 | [miguelmota](https://github.com/miguelmota) |
322  | 1 | [dtothefp](https://github.com/dtothefp) |
323  | 1 | [ksheedlo](https://github.com/ksheedlo) |
324  | 1 | [pdehaan](https://github.com/pdehaan) |
325  | 1 | [laggingreflex](https://github.com/laggingreflex) |
326  | 1 | [charlike](https://github.com/charlike) |
327  
328  ### Author
329  
330  **Jon Schlinkert**
331  
332  * [github/jonschlinkert](https://github.com/jonschlinkert)
333  * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
334  
335  ### License
336  
337  Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
338  Released under the [MIT License](LICENSE).
339  
340  ***
341  
342  _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 13, 2017._