websocket快速操作工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.9 KiB

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