kind-of

中文文档
6.0.3发布于 2020/01/17MITCommonJS零依赖近 30 天下载 4.4亿

获取一个值的本地类型 .

中文使用文档
文档目录13 节
  1. 安装
  2. 为什么用这个?
  3. 使用量
  4. 基准
  5. 优化
  6. 更好的类型检查
  7. 类型 独立
  8. 类型 独立
  9. 关于
  10. 相关项目
  11. 贡献者
  12. 作者
  13. 许可证

类型 NPM version NPM monthly downloads NPM total downloads Linux Build Status

获取一个值的本地类型 .

请考虑跟随这个项目的作者, 乔恩·斯林克特,并考虑主演该项目以显示你的:心:和支持.

安装

安装方式 无数字 :

$ npm install --save kind-of

安装方式 箭头

$ bower install kind-of --save

为什么用这个?

  1. 这么快 | 优化
  2. 更好的类型检查

使用量

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. 有一些东西导致这种性能优势,没有硬而快的规则,但几乎所有代码库中都简单而可重复:

  1. 首先优化最快和最常见的使用案例。 当然,这将会改变 从项目到项目, 但我花了一些时间来理解为什么 typeof 支票被用在我的图书馆 我经常使用的其他图书馆.
  2. 围绕瓶颈实现优化 - 换句话说,执行条件的顺序是显著的,因为每次检查都和之前的失败检查一样快. 这里,迄今为止最大的瓶颈是检查平地物体(由该物体所创建) Object 建筑师). 我选择了通过消灭过程而不是通过前面的野蛮武力(例如使用类似的东西)来实现这种检查 val.constructor.name),这样,所有其他类型的检查都不会受到处罚.
  3. 不要做不必要处理 - 为什么 .slice(8, -1).toLowerCase(); 只是为了得到消息 regex它的更快做到这一点 if (type === '[object RegExp]') return 'regex'
  4. 没有理由在微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

您可能也对这些项目感兴趣:

贡献者

| 提交 | 贡献者 |
| -- -- -- -- -- -- --
102个 扬施林克特 |
| 3 | 域码 |
|2| 米格尔莫塔 |
| 1 | 鸽子 |
| 1 | dtothefp 软件 |
| 1 | 扬·斯通克勒 |
| 1 | 克谢德洛 |
| 1 | 脚下 |
| 1 | 后退读取 |
| 1 | tunncko 企业 |
| 1 | 霞风9号 |

作者

乔恩·斯林克特

许可证

版权:2020年, 乔恩·斯林克特。 。 。 . 释放 MIT 许可证。 。 。 .


此文件由 动词基因读取器v0.8.0,于2020年1月16日查阅.

版本与发布记录

kind-of 共发布 27 个版本,首次发布于 2014/09/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.3latest2020/01/176 年前
6.0.22017/12/028 年前
6.0.12017/11/198 年前
6.0.02017/10/139 年前
5.1.02017/10/139 年前
5.0.22017/08/029 年前

关于 kind-of 的常见问题

kind-of 是什么?

获取一个值的本地类型 .

如何安装 kind-of?

在项目目录运行 npm install kind-of,或使用 pnpm add / yarn add / bun add。

kind-of 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/kind-of 是否存在。

kind-of 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

kind-of 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.10.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

kind-of 可以商用吗?

kind-of 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

kind-of 的下载量有多少?

根据 npm 官方下载统计,kind-of 在 2026/10/05 – 2026/11/03 这 30 天内被下载 438,975,338 次,日均约 14,632,511 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

kind-of 还在维护吗?

最新版本 6.0.3 发布于 2020/01/17(6 年前);至今共发布 27 个版本,其中过去 12 个月发布了 0 个;首次发布于 2014/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。