类型

获取一个值的本地类型 .
请考虑跟随这个项目的作者, 乔恩·斯林克特,并考虑主演该项目以显示你的:心:和支持.
安装
安装方式 无数字 :
$ npm install --save kind-of
安装方式 箭头
$ bower install kind-of --save
为什么用这个?
使用量
es5,es6,浏览器准备好
var kindOf = require('kind-of');
kindOf(undefined);
//=> 'undefined'
kindOf(null);
//=> 'null'
kindOf(true);
//=> 'boolean'
kindOf(false);
//=> 'boolean'
kindOf(new Buffer(''));
//=> 'buffer'
kindOf(42);
//=> 'number'
kindOf('str');
//=> 'string'
kindOf(arguments);
//=> 'arguments'
kindOf({});
//=> 'object'
kindOf(Object.create(null));
//=> 'object'
kindOf(new Test());
//=> 'object'
kindOf(new Date());
//=> 'date'
kindOf([1, 2, 3]);
//=> 'array'
kindOf(/foo/);
//=> 'regexp'
kindOf(new RegExp('foo'));
//=> 'regexp'
kindOf(new Error('error'));
//=> 'error'
kindOf(function () {});
//=> 'function'
kindOf(function * () {});
//=> 'generatorfunction'
kindOf(Symbol('str'));
//=> 'symbol'
kindOf(new Map());
//=> 'map'
kindOf(new WeakMap());
//=> 'weakmap'
kindOf(new Set());
//=> 'set'
kindOf(new WeakSet());
//=> 'weakset'
kindOf(new Int8Array());
//=> 'int8array'
kindOf(new Uint8Array());
//=> 'uint8array'
kindOf(new Uint8ClampedArray());
//=> 'uint8clampedarray'
kindOf(new Int16Array());
//=> 'int16array'
kindOf(new Uint16Array());
//=> 'uint16array'
kindOf(new Int32Array());
//=> 'int32array'
kindOf(new Uint32Array());
//=> 'uint32array'
kindOf(new Float32Array());
//=> 'float32array'
kindOf(new Float64Array());
//=> 'float64array'
基准
# arguments (32 bytes)
kind-of x 17,024,098 ops/sec ±1.90% (86 runs sampled)
lib-type-of x 11,926,235 ops/sec ±1.34% (83 runs sampled)
lib-typeof x 9,245,257 ops/sec ±1.22% (87 runs sampled)
fastest is kind-of (by 161% avg)
# array (22 bytes)
kind-of x 17,196,492 ops/sec ±1.07% (88 runs sampled)
lib-type-of x 8,838,283 ops/sec ±1.02% (87 runs sampled)
lib-typeof x 8,677,848 ops/sec ±0.87% (87 runs sampled)
fastest is kind-of (by 196% avg)
# boolean (24 bytes)
kind-of x 16,841,600 ops/sec ±1.10% (86 runs sampled)
lib-type-of x 8,096,787 ops/sec ±0.95% (87 runs sampled)
lib-typeof x 8,423,345 ops/sec ±1.15% (86 runs sampled)
fastest is kind-of (by 204% avg)
# buffer (38 bytes)
kind-of x 14,848,060 ops/sec ±1.05% (86 runs sampled)
lib-type-of x 3,671,577 ops/sec ±1.49% (87 runs sampled)
lib-typeof x 8,360,236 ops/sec ±1.24% (86 runs sampled)
fastest is kind-of (by 247% avg)
# date (30 bytes)
kind-of x 16,067,761 ops/sec ±1.58% (86 runs sampled)
lib-type-of x 8,954,436 ops/sec ±1.40% (87 runs sampled)
lib-typeof x 8,488,307 ops/sec ±1.51% (84 runs sampled)
fastest is kind-of (by 184% avg)
# error (36 bytes)
kind-of x 9,634,090 ops/sec ±1.12% (89 runs sampled)
lib-type-of x 7,735,624 ops/sec ±1.32% (86 runs sampled)
lib-typeof x 7,442,160 ops/sec ±1.11% (90 runs sampled)
fastest is kind-of (by 127% avg)
# function (34 bytes)
kind-of x 10,031,494 ops/sec ±1.27% (86 runs sampled)
lib-type-of x 9,502,757 ops/sec ±1.17% (89 runs sampled)
lib-typeof x 8,278,985 ops/sec ±1.08% (88 runs sampled)
fastest is kind-of (by 113% avg)
# null (24 bytes)
kind-of x 18,159,808 ops/sec ±1.92% (86 runs sampled)
lib-type-of x 12,927,635 ops/sec ±1.01% (88 runs sampled)
lib-typeof x 7,958,234 ops/sec ±1.21% (89 runs sampled)
fastest is kind-of (by 174% avg)
# number (22 bytes)
kind-of x 17,846,779 ops/sec ±0.91% (85 runs sampled)
lib-type-of x 3,316,636 ops/sec ±1.19% (86 runs sampled)
lib-typeof x 2,329,477 ops/sec ±2.21% (85 runs sampled)
fastest is kind-of (by 632% avg)
# object-plain (47 bytes)
kind-of x 7,085,155 ops/sec ±1.05% (88 runs sampled)
lib-type-of x 8,870,930 ops/sec ±1.06% (83 runs sampled)
lib-typeof x 8,716,024 ops/sec ±1.05% (87 runs sampled)
fastest is lib-type-of (by 112% avg)
# regex (25 bytes)
kind-of x 14,196,052 ops/sec ±1.65% (84 runs sampled)
lib-type-of x 9,554,164 ops/sec ±1.25% (88 runs sampled)
lib-typeof x 8,359,691 ops/sec ±1.07% (87 runs sampled)
fastest is kind-of (by 158% avg)
# string (33 bytes)
kind-of x 16,131,428 ops/sec ±1.41% (85 runs sampled)
lib-type-of x 7,273,172 ops/sec ±1.05% (87 runs sampled)
lib-typeof x 7,382,635 ops/sec ±1.17% (85 runs sampled)
fastest is kind-of (by 220% avg)
# symbol (34 bytes)
kind-of x 17,011,537 ops/sec ±1.24% (86 runs sampled)
lib-type-of x 3,492,454 ops/sec ±1.23% (89 runs sampled)
lib-typeof x 7,471,235 ops/sec ±2.48% (87 runs sampled)
fastest is kind-of (by 310% avg)
# template-strings (36 bytes)
kind-of x 15,434,250 ops/sec ±1.46% (83 runs sampled)
lib-type-of x 7,157,907 ops/sec ±0.97% (87 runs sampled)
lib-typeof x 7,517,986 ops/sec ±0.92% (86 runs sampled)
fastest is kind-of (by 210% avg)
# undefined (29 bytes)
kind-of x 19,167,115 ops/sec ±1.71% (87 runs sampled)
lib-type-of x 15,477,740 ops/sec ±1.63% (85 runs sampled)
lib-typeof x 19,075,495 ops/sec ±1.17% (83 runs sampled)
fastest is lib-typeof,kind-of
优化
在8个案件中的7个案件中,这个图书馆比列入基准的其他顶级图书馆快了2x-10x. 有一些东西导致这种性能优势,没有硬而快的规则,但几乎所有代码库中都简单而可重复:
- 首先优化最快和最常见的使用案例。 当然,这将会改变 从项目到项目, 但我花了一些时间来理解为什么
typeof支票被用在我的图书馆 我经常使用的其他图书馆. - 围绕瓶颈实现优化 - 换句话说,执行条件的顺序是显著的,因为每次检查都和之前的失败检查一样快. 这里,迄今为止最大的瓶颈是检查平地物体(由该物体所创建)
Object建筑师). 我选择了通过消灭过程而不是通过前面的野蛮武力(例如使用类似的东西)来实现这种检查val.constructor.name),这样,所有其他类型的检查都不会受到处罚. - 不要做不必要处理 - 为什么
.slice(8, -1).toLowerCase();只是为了得到消息regex它的更快做到这一点if (type === '[object RegExp]') return 'regex' - 没有理由在微lib中尽可能地使代码为terse,仅仅为了让代码更短而赢得分数. 偏好表演密码比特尔斯密码好 你总是只用一个
require()语句,不管代码是怎样写的.
更好的类型检查
似乎比我看过的其他类型检查液态更一致 例如,这里有一些不同的结果来自其他民众的解放:
类型 独立
无法正确识别自定义构造器实例( 相当常见 ) :
var typeOf = require('typeof');
function Test() {}
console.log(typeOf(new Test()));
//=> 'test'
回返 object 改为 arguments数字 :
function foo() {
console.log(typeOf(arguments)) //=> 'object'
}
foo();
类型 独立
返回错误 object 用于生成器函数、缓冲器、, Map, (中文(简体) ), Set, (中文(简体) ), WeakMap 财务报告和审定财务报表 WeakSet数字 :
function * foo() {}
console.log(typeOf(foo));
//=> 'object'
console.log(typeOf(new Buffer('')));
//=> 'object'
console.log(typeOf(new Map()));
//=> 'object'
console.log(typeOf(new Set()));
//=> 'object'
console.log(typeOf(new WeakMap()));
//=> 'object'
console.log(typeOf(new WeakSet()));
//=> 'object'
关于
捐款
拉出请求和星星总是受欢迎的. 对于错误和特性请求, 请创建一个问题。 。 。 .
运行测试
运行和审查单元测试是熟悉图书馆及其API的好办法。 您可以使用以下命令安装依赖性并运行测试:
$ npm install && npm test
大楼文件
(此工程的 readme.md 由 动词请不要直接编辑读取器。 读取器的任何改动必须在 动词 md 读取我的模板。 )
要生成读取, 请运行以下命令 :
$ npm install -g verbose/verb#dev verb-generate-readme && verb
相关项目
您可能也对这些项目感兴趣:
- 正在显示: 回归
true如果给定的字符串看起来像是光彩图案或外形图案.. 更多 | 主页 - 是数: 如果一个数字或字符串值是有限数字,则返回正数。 对regex有用.. 更多 | 主页
- 是原始内容: 回归
true如果数值是原始的 主页
贡献者
| 提交 | 贡献者 |
| -- -- -- -- -- -- --
102个 扬施林克特 |
| 3 | 域码 |
|2| 米格尔莫塔 |
| 1 | 鸽子 |
| 1 | dtothefp 软件 |
| 1 | 扬·斯通克勒 |
| 1 | 克谢德洛 |
| 1 | 脚下 |
| 1 | 后退读取 |
| 1 | tunncko 企业 |
| 1 | 霞风9号 |
作者
乔恩·斯林克特
许可证
版权:2020年, 乔恩·斯林克特。 。 。 . 释放 MIT 许可证。 。 。 .
此文件由 动词基因读取器v0.8.0,于2020年1月16日查阅.