Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kind-of

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kind-of - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

LICENSE

58

index.js

@@ -11,3 +11,4 @@ var toString = Object.prototype.toString;

module.exports = function kindOf(val) {
if (val === undefined) {
// primitivies
if (typeof val === 'undefined') {
return 'undefined';

@@ -21,27 +22,64 @@ }

}
if (typeof val !== 'object') {
return typeof val;
if (typeof val === 'string' || val instanceof String) {
return 'string'
}
if (Array.isArray(val)) {
if (typeof val === 'number' || val instanceof Number) {
return 'number'
}
// functions
if (typeof val === 'function' || val instanceof Function) {
return 'function'
}
// array
if (typeof Array.isArray !== 'undefined' && Array.isArray(val)) {
return 'array';
}
// check for instances of RegExp and Date before calling `toString`
if (val instanceof RegExp) {
return 'regexp';
}
if (val instanceof Date) {
return 'date';
}
// other objects
var type = toString.call(val);
if (val instanceof RegExp || type === '[object RegExp]') {
if (type === '[object RegExp]') {
return 'regexp';
}
if (val instanceof Date || type === '[object Date]') {
if (type === '[object Date]') {
return 'date';
}
if (type === '[object Function]') {
return 'function';
}
if (type === '[object Arguments]') {
return 'arguments';
}
// buffer
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(val)) {
return 'buffer';
}
return type.slice(8, -1).toLowerCase();
// es6: Map, WeakMap, Set, WeakSet
if (type === '[object Set]') {
return 'set';
}
if (type === '[object WeakSet]') {
return 'weakset';
}
if (type === '[object Map]') {
return 'map';
}
if (type === '[object WeakMap]') {
return 'weakmap';
}
if (type === '[object Symbol]') {
return 'symbol';
}
// must be a plain object
return 'object';
};

24

package.json
{
"name": "kind-of",
"description": "Get the native type of a value.",
"version": "1.1.0",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/kind-of",
"author": {
"name": "Jon Schlinkert",
"url": "https://github.com/jonschlinkert"
},
"repository": {
"type": "git",
"url": "git://github.com/jonschlinkert/kind-of.git"
},
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/kind-of",
"bugs": {
"url": "https://github.com/jonschlinkert/kind-of/issues"
},
"license": {
"type": "MIT",
"url": "https://github.com/jonschlinkert/kind-of/blob/master/LICENSE"
},
"files": ["index.js"],
"license": "MIT",
"files": [
"index.js"
],
"main": "index.js",

@@ -33,2 +26,3 @@ "engines": {

"glob": "^4.3.5",
"mocha": "^2.2.5",
"should": "^4.6.1",

@@ -60,2 +54,2 @@ "type-of": "^2.0.1",

]
}
}

@@ -1,13 +0,25 @@

# 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)
# 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)
> Get the native type of a value.
## Install with [npm](npmjs.org)
[](#optimizations)**What makes this so fast?**
```bash
npm i kind-of --save
## Install
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i kind-of --save
```
Install with [bower](http://bower.io/)
```sh
$ bower install kind-of --save
```
## Usage
> es5, browser and es6 ready
```js

@@ -40,5 +52,8 @@ var kindOf = require('kind-of');

kindOf("string");
kindOf('str');
//=> 'string'
kindOf(new String('str'));
//=> 'string'
kindOf(arguments);

@@ -50,2 +65,5 @@ //=> 'arguments'

kindOf(Object.create(null));
//=> 'object'
kindOf(new Test());

@@ -75,84 +93,123 @@ //=> 'object'

kindOf(function * () {});
//=> 'function'
kindOf(new Function());
//=> 'function'
```
kindOf(new Map());
//=> 'map'
## Run tests
kindOf(new WeakMap());
//=> 'weakmap'
Install dev dependencies:
kindOf(new Set());
//=> 'set'
```bash
npm i -d && npm test
kindOf(new WeakSet());
//=> 'weakset'
kindOf(Symbol('str'));
//=> 'symbol'
```
## Related projects
* [is-number](https://github.com/jonschlinkert/is-number): Returns true if the value is a number. comprehensive tests.
* [isobject](https://github.com/jonschlinkert/isobject): Returns true if the value is an object and not an array or null.
* [is-primitive](https://github.com/jonschlinkert/is-primitive): Returns `true` if the value is a primitive.
* [is-plain-object](https://github.com/jonschlinkert/is-plain-object): Returns true if an object was created by the `Object` constructor.
* [is-match](https://github.com/jonschlinkert/is-match): Create a matching function from a glob pattern, regex, string, array or function.
## Benchmarks
Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of).
Note that performaces is slower for es6 features `Map`, `WeakMap`, `Set` and `WeakSet`.
```bash
#1: array.js
kind-of x 21,578,944 ops/sec ±1.01% (97 runs sampled)
(lib) type-of x 4,593,840 ops/sec ±0.76% (92 runs sampled)
(lib) typeof x 5,786,776 ops/sec ±0.71% (97 runs sampled)
#1: array
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)
#2: boolean.js
kind-of x 25,189,600 ops/sec ±0.60% (97 runs sampled)
(lib) type-of x 2,751,076 ops/sec ±0.78% (100 runs sampled)
(lib) typeof x 4,390,312 ops/sec ±0.61% (99 runs sampled)
#2: boolean
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)
#3: date.js
kind-of x 8,862,303 ops/sec ±0.77% (99 runs sampled)
(lib) type-of x 6,239,662 ops/sec ±0.67% (94 runs sampled)
(lib) typeof x 6,180,922 ops/sec ±0.59% (97 runs sampled)
#3: date
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)
#4: function.js
kind-of x 19,685,336 ops/sec ±1.67% (95 runs sampled)
(lib) type-of x 6,648,551 ops/sec ±0.93% (95 runs sampled)
(lib) typeof x 6,631,967 ops/sec ±1.05% (92 runs sampled)
#4: function
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)
#5: null.js
kind-of x 24,155,010 ops/sec ±0.95% (91 runs sampled)
(lib) type-of x 12,854,583 ops/sec ±0.69% (94 runs sampled)
(lib) typeof x 8,182,952 ops/sec ±0.48% (99 runs sampled)
#5: null
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)
#6: number.js
kind-of x 20,993,521 ops/sec ±0.37% (98 runs sampled)
(lib) type-of x 2,112,716 ops/sec ±0.73% (96 runs sampled)
(lib) typeof x 4,492,943 ops/sec ±0.68% (96 runs sampled)
#6: number
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)
#7: object.js
kind-of x 3,686,169 ops/sec ±0.85% (96 runs sampled)
(lib) type-of x 3,661,833 ops/sec ±0.73% (98 runs sampled)
(lib) typeof x 6,159,847 ops/sec ±0.72% (98 runs sampled)
#7: object
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)
#8: regex.js
kind-of x 10,780,535 ops/sec ±0.75% (95 runs sampled)
(lib) type-of x 5,380,781 ops/sec ±0.83% (92 runs sampled)
(lib) typeof x 5,852,558 ops/sec ±0.67% (95 runs sampled)
#8: regex
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)
#9: string.js
kind-of x 19,713,570 ops/sec ±0.69% (91 runs sampled)
(lib) type-of x 4,017,753 ops/sec ±0.85% (98 runs sampled)
(lib) typeof x 4,370,984 ops/sec ±0.62% (100 runs sampled)
#9: string
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)
#10: undef.js
kind-of x 23,250,387 ops/sec ±0.88% (91 runs sampled)
(lib) type-of x 13,725,183 ops/sec ±0.62% (91 runs sampled)
(lib) typeof x 20,549,334 ops/sec ±0.74% (97 runs sampled)
#10: undef
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:
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.
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.
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'`
## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/kind-of/issues/new)
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license
Copyright © 2014-2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.
***
_This file was generated by [verb](https://github.com/assemble/verb) on February 09, 2015._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 31, 2015._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc