Projekt

Obecné

Profil

Stáhnout (7.9 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
# 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
## Install
6
7
Install with [npm](https://www.npmjs.com/):
8
9
```sh
10
$ npm install --save kind-of
11
```
12
13
## Install
14
15
Install with [bower](https://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
26
var kindOf = require('kind-of');
27
28
kindOf(undefined);
29
//=> 'undefined'
30
31
kindOf(null);
32
//=> 'null'
33
34
kindOf(true);
35
//=> 'boolean'
36
37
kindOf(false);
38
//=> 'boolean'
39
40
kindOf(new Boolean(true));
41
//=> 'boolean'
42
43
kindOf(new Buffer(''));
44
//=> 'buffer'
45
46
kindOf(42);
47
//=> 'number'
48
49
kindOf(new Number(42));
50
//=> 'number'
51
52
kindOf('str');
53
//=> 'string'
54
55
kindOf(new String('str'));
56
//=> 'string'
57
58
kindOf(arguments);
59
//=> 'arguments'
60
61
kindOf({});
62
//=> 'object'
63
64
kindOf(Object.create(null));
65
//=> 'object'
66
67
kindOf(new Test());
68
//=> 'object'
69
70
kindOf(new Date());
71
//=> 'date'
72
73
kindOf([]);
74
//=> 'array'
75
76
kindOf([1, 2, 3]);
77
//=> 'array'
78
79
kindOf(new Array());
80
//=> 'array'
81
82
kindOf(/foo/);
83
//=> 'regexp'
84
85
kindOf(new RegExp('foo'));
86
//=> 'regexp'
87
88
kindOf(function () {});
89
//=> 'function'
90
91
kindOf(function * () {});
92
//=> 'function'
93
94
kindOf(new Function());
95
//=> 'function'
96
97
kindOf(new Map());
98
//=> 'map'
99
100
kindOf(new WeakMap());
101
//=> 'weakmap'
102
103
kindOf(new Set());
104
//=> 'set'
105
106
kindOf(new WeakSet());
107
//=> 'weakset'
108
109
kindOf(Symbol('str'));
110
//=> 'symbol'
111
112
kindOf(new Int8Array());
113
//=> 'int8array'
114
115
kindOf(new Uint8Array());
116
//=> 'uint8array'
117
118
kindOf(new Uint8ClampedArray());
119
//=> 'uint8clampedarray'
120
121
kindOf(new Int16Array());
122
//=> 'int16array'
123
124
kindOf(new Uint16Array());
125
//=> 'uint16array'
126
127
kindOf(new Int32Array());
128
//=> 'int32array'
129
130
kindOf(new Uint32Array());
131
//=> 'uint32array'
132
133
kindOf(new Float32Array());
134
//=> 'float32array'
135
136
kindOf(new Float64Array());
137
//=> 'float64array'
138
```
139
140
## Benchmarks
141
142
Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of).
143
Note that performaces is slower for es6 features `Map`, `WeakMap`, `Set` and `WeakSet`.
144
145
```bash
146
#1: array
147
  current x 23,329,397 ops/sec ±0.82% (94 runs sampled)
148
  lib-type-of x 4,170,273 ops/sec ±0.55% (94 runs sampled)
149
  lib-typeof x 9,686,935 ops/sec ±0.59% (98 runs sampled)
150
151
#2: boolean
152
  current x 27,197,115 ops/sec ±0.85% (94 runs sampled)
153
  lib-type-of x 3,145,791 ops/sec ±0.73% (97 runs sampled)
154
  lib-typeof x 9,199,562 ops/sec ±0.44% (99 runs sampled)
155
156
#3: date
157
  current x 20,190,117 ops/sec ±0.86% (92 runs sampled)
158
  lib-type-of x 5,166,970 ops/sec ±0.74% (94 runs sampled)
159
  lib-typeof x 9,610,821 ops/sec ±0.50% (96 runs sampled)
160
161
#4: function
162
  current x 23,855,460 ops/sec ±0.60% (97 runs sampled)
163
  lib-type-of x 5,667,740 ops/sec ±0.54% (100 runs sampled)
164
  lib-typeof x 10,010,644 ops/sec ±0.44% (100 runs sampled)
165
166
#5: null
167
  current x 27,061,047 ops/sec ±0.97% (96 runs sampled)
168
  lib-type-of x 13,965,573 ops/sec ±0.62% (97 runs sampled)
169
  lib-typeof x 8,460,194 ops/sec ±0.61% (97 runs sampled)
170
171
#6: number
172
  current x 25,075,682 ops/sec ±0.53% (99 runs sampled)
173
  lib-type-of x 2,266,405 ops/sec ±0.41% (98 runs sampled)
174
  lib-typeof x 9,821,481 ops/sec ±0.45% (99 runs sampled)
175
176
#7: object
177
  current x 3,348,980 ops/sec ±0.49% (99 runs sampled)
178
  lib-type-of x 3,245,138 ops/sec ±0.60% (94 runs sampled)
179
  lib-typeof x 9,262,952 ops/sec ±0.59% (99 runs sampled)
180
181
#8: regex
182
  current x 21,284,827 ops/sec ±0.72% (96 runs sampled)
183
  lib-type-of x 4,689,241 ops/sec ±0.43% (100 runs sampled)
184
  lib-typeof x 8,957,593 ops/sec ±0.62% (98 runs sampled)
185
186
#9: string
187
  current x 25,379,234 ops/sec ±0.58% (96 runs sampled)
188
  lib-type-of x 3,635,148 ops/sec ±0.76% (93 runs sampled)
189
  lib-typeof x 9,494,134 ops/sec ±0.49% (98 runs sampled)
190
191
#10: undef
192
  current x 27,459,221 ops/sec ±1.01% (93 runs sampled)
193
  lib-type-of x 14,360,433 ops/sec ±0.52% (99 runs sampled)
194
  lib-typeof x 23,202,868 ops/sec ±0.59% (94 runs sampled)
195
196
```
197
198
## Optimizations
199
200
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:
201
202
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.
203
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.
204
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'`
205
206
## About
207
208
### Related projects
209
210
* [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")
211
* [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.")
212
* [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. ")
213
214
### Contributing
215
216
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
217
218
### Contributors
219
220
| **Commits** | **Contributor** | 
221
| --- | --- |
222
| 59 | [jonschlinkert](https://github.com/jonschlinkert) |
223
| 2 | [miguelmota](https://github.com/miguelmota) |
224
| 1 | [dtothefp](https://github.com/dtothefp) |
225
| 1 | [ksheedlo](https://github.com/ksheedlo) |
226
| 1 | [pdehaan](https://github.com/pdehaan) |
227
| 1 | [laggingreflex](https://github.com/laggingreflex) |
228
229
### Building docs
230
231
_(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.)_
232
233
To generate the readme, run the following command:
234
235
```sh
236
$ npm install -g verbose/verb#dev verb-generate-readme && verb
237
```
238
239
### Running tests
240
241
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:
242
243
```sh
244
$ npm install && npm test
245
```
246
247
### Author
248
249
**Jon Schlinkert**
250
251
* [github/jonschlinkert](https://github.com/jonschlinkert)
252
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
253
254
### License
255
256
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
257
Released under the [MIT License](LICENSE).
258
259
***
260
261
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 16, 2017._