From 0d6c1d8d4343c4845f516409ac0a9f2dca0b4e9d Mon Sep 17 00:00:00 2001 From: Pedro Batista Date: Wed, 26 Sep 2018 08:10:09 -0300 Subject: [PATCH 1/2] Added support for async functions and dataviews + tests --- benchmark/index.js | 2 +- benchmark/stats.md | 162 +++++++++++++++++++++++++--------------- browser.js | 63 +++++++++++++++- index.js | 18 +++-- test/es6/.eslintrc.json | 5 ++ test/es6/index.js | 30 +++++++- 6 files changed, 207 insertions(+), 73 deletions(-) create mode 100644 test/es6/.eslintrc.json diff --git a/benchmark/index.js b/benchmark/index.js index 9f1fca6..77c3b4d 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -9,7 +9,7 @@ suite.run({ code: 'code/{kind-of,lib-*}.js' }) .then(function(stats) { - write.sync(path.join(__dirname, 'stats.json'), JSON.stringify(stats, null, 2)) + write.sync(path.join(__dirname, 'stats.json'), JSON.stringify(stats, null, 2)); write.sync(path.join(__dirname, 'stats.md'), suite.render(stats)); }) .catch(console.error); diff --git a/benchmark/stats.md b/benchmark/stats.md index 6b1afc0..c47e4e3 100644 --- a/benchmark/stats.md +++ b/benchmark/stats.md @@ -1,104 +1,146 @@ # 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) + kind-of x 9,196,872 ops/sec ±2.95% (81 runs sampled) + lib-type-of x 8,982,697 ops/sec ±2.02% (83 runs sampled) + lib-typeof x 6,266,723 ops/sec ±2.18% (84 runs sampled) - fastest is kind-of (by 161% avg) + fastest is kind-of (by 121% 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) + kind-of x 8,114,554 ops/sec ±3.84% (74 runs sampled) + lib-type-of x 8,579,719 ops/sec ±2.31% (76 runs sampled) + lib-typeof x 5,565,546 ops/sec ±3.30% (76 runs sampled) - fastest is kind-of (by 196% avg) + fastest is lib-type-of (by 125% 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) + kind-of x 9,493,477 ops/sec ±1.27% (85 runs sampled) + lib-type-of x 6,675,738 ops/sec ±1.93% (86 runs sampled) + lib-typeof x 5,964,909 ops/sec ±1.63% (84 runs sampled) - fastest is kind-of (by 204% avg) + fastest is kind-of (by 150% 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) + kind-of x 7,927,133 ops/sec ±2.73% (82 runs sampled) + lib-type-of x 2,631,992 ops/sec ±1.94% (78 runs sampled) + lib-typeof x 5,490,871 ops/sec ±1.96% (81 runs sampled) - fastest is kind-of (by 247% avg) + fastest is kind-of (by 195% 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) + kind-of x 7,223,202 ops/sec ±3.51% (77 runs sampled) + lib-type-of x 7,595,355 ops/sec ±2.79% (77 runs sampled) + lib-typeof x 5,255,246 ops/sec ±2.22% (81 runs sampled) - fastest is kind-of (by 184% avg) + fastest is lib-type-of (by 122% 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) + kind-of x 7,838,819 ops/sec ±1.39% (87 runs sampled) + lib-type-of x 6,224,852 ops/sec ±1.40% (87 runs sampled) + lib-typeof x 4,936,830 ops/sec ±3.82% (81 runs sampled) - fastest is kind-of (by 127% avg) + fastest is kind-of (by 140% 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) + kind-of x 5,558,735 ops/sec ±3.84% (73 runs sampled) + lib-type-of x 7,394,550 ops/sec ±5.67% (76 runs sampled) + lib-typeof x 4,463,217 ops/sec ±4.18% (75 runs sampled) - fastest is kind-of (by 113% avg) + fastest is lib-type-of (by 148% avg) + +# generator (39 bytes) + kind-of x 6,487,003 ops/sec ±1.38% (84 runs sampled) + lib-type-of x 4,926,155 ops/sec ±1.93% (83 runs sampled) + lib-typeof x 4,758,900 ops/sec ±1.23% (85 runs sampled) + + fastest is kind-of (by 134% avg) + +# map (30 bytes) + kind-of x 3,868,266 ops/sec ±2.35% (81 runs sampled) + lib-type-of x 3,720,270 ops/sec ±2.07% (83 runs sampled) + lib-typeof x 5,243,265 ops/sec ±3.18% (79 runs sampled) + + fastest is lib-typeof (by 138% 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) + kind-of x 9,206,941 ops/sec ±1.36% (82 runs sampled) + lib-type-of x 7,710,756 ops/sec ±1.40% (86 runs sampled) + lib-typeof x 5,050,484 ops/sec ±3.15% (80 runs sampled) - fastest is kind-of (by 174% avg) + fastest is kind-of (by 144% 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) + kind-of x 9,063,676 ops/sec ±1.65% (84 runs sampled) + lib-type-of x 1,536,407 ops/sec ±3.45% (80 runs sampled) + lib-typeof x 1,516,412 ops/sec ±4.32% (75 runs sampled) + + fastest is kind-of (by 594% avg) + +# object-instance (22 bytes) + kind-of x 3,517,921 ops/sec ±2.55% (85 runs sampled) + lib-type-of x 5,715,924 ops/sec ±1.32% (86 runs sampled) + lib-typeof x 5,156,627 ops/sec ±2.01% (84 runs sampled) - fastest is kind-of (by 632% avg) + fastest is lib-type-of (by 132% 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) + kind-of x 2,873,161 ops/sec ±3.85% (80 runs sampled) + lib-type-of x 5,195,766 ops/sec ±0.92% (88 runs sampled) + lib-typeof x 5,333,247 ops/sec ±1.83% (84 runs sampled) - fastest is lib-type-of (by 112% avg) + fastest is lib-typeof (by 132% 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) + kind-of x 4,396,287 ops/sec ±1.57% (86 runs sampled) + lib-type-of x 8,237,479 ops/sec ±1.97% (83 runs sampled) + lib-typeof x 5,003,411 ops/sec ±2.84% (86 runs sampled) - fastest is kind-of (by 158% avg) + fastest is lib-type-of (by 175% avg) + +# set (30 bytes) + kind-of x 3,056,128 ops/sec ±2.22% (86 runs sampled) + lib-type-of x 3,640,163 ops/sec ±1.03% (89 runs sampled) + lib-typeof x 5,453,536 ops/sec ±1.49% (85 runs sampled) + + fastest is lib-typeof (by 163% 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) + kind-of x 9,045,366 ops/sec ±1.85% (85 runs sampled) + lib-type-of x 7,752,660 ops/sec ±1.31% (87 runs sampled) + lib-typeof x 5,083,803 ops/sec ±1.88% (84 runs sampled) - fastest is kind-of (by 220% avg) + fastest is kind-of (by 141% 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) + kind-of x 9,395,197 ops/sec ±1.20% (85 runs sampled) + lib-type-of x 2,274,160 ops/sec ±3.67% (83 runs sampled) + lib-typeof x 5,128,325 ops/sec ±2.13% (85 runs sampled) - fastest is kind-of (by 310% avg) + fastest is kind-of (by 254% 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) + kind-of x 8,964,511 ops/sec ±1.69% (82 runs sampled) + lib-type-of x 7,655,202 ops/sec ±2.74% (87 runs sampled) + lib-typeof x 5,155,728 ops/sec ±1.38% (86 runs sampled) - fastest is kind-of (by 210% avg) + fastest is kind-of (by 140% 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) + kind-of x 9,663,832 ops/sec ±1.37% (85 runs sampled) + lib-type-of x 7,566,354 ops/sec ±2.25% (85 runs sampled) + lib-typeof x 9,540,619 ops/sec ±1.44% (85 runs sampled) + + fastest is kind-of (by 113% avg) + +# weakmap (34 bytes) + kind-of x 2,906,283 ops/sec ±2.18% (83 runs sampled) + lib-type-of x 4,248,298 ops/sec ±1.11% (88 runs sampled) + lib-typeof x 5,238,962 ops/sec ±2.09% (83 runs sampled) + + fastest is lib-typeof (by 146% avg) + +# weakset (34 bytes) + kind-of x 3,009,178 ops/sec ±1.22% (86 runs sampled) + lib-type-of x 4,203,227 ops/sec ±1.86% (85 runs sampled) + lib-typeof x 5,286,605 ops/sec ±1.26% (84 runs sampled) - fastest is lib-typeof,kind-of + fastest is lib-typeof (by 147% avg) diff --git a/browser.js b/browser.js index 82998d8..d56aa44 100644 --- a/browser.js +++ b/browser.js @@ -1,7 +1,7 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.index = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; + },{}]},{},[1])(1) -}); \ No newline at end of file +}); diff --git a/index.js b/index.js index aa2bb39..fb695a0 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,7 @@ module.exports = function kindOf(val) { if (type === 'string') return 'string'; if (type === 'number') return 'number'; if (type === 'symbol') return 'symbol'; - if (type === 'function') { - return isGeneratorFn(val) ? 'generatorfunction' : 'function'; - } + if (type === 'function') return functionType(val); if (isArray(val)) return 'array'; if (isBuffer(val)) return 'buffer'; @@ -44,6 +42,9 @@ module.exports = function kindOf(val) { case 'Uint32Array': return 'uint32array'; case 'Float32Array': return 'float32array'; case 'Float64Array': return 'float64array'; + + // Data views + case 'DataView': return 'dataview'; } if (isGeneratorObj(val)) { @@ -93,10 +94,6 @@ function isRegexp(val) { && typeof val.global === 'boolean'; } -function isGeneratorFn(name, val) { - return ctorName(name) === 'GeneratorFunction'; -} - function isGeneratorObj(val) { return typeof val.throw === 'function' && typeof val.return === 'function' @@ -127,3 +124,10 @@ function isBuffer(val) { } return false; } + +function functionType(name) { + if (ctorName(name) === 'GeneratorFunction') return 'generatorfunction'; + if (toString.call(name) === '[object AsyncFunction]') return 'asyncfunction'; + + return 'function'; +} diff --git a/test/es6/.eslintrc.json b/test/es6/.eslintrc.json new file mode 100644 index 0000000..62b2994 --- /dev/null +++ b/test/es6/.eslintrc.json @@ -0,0 +1,5 @@ +{ + "parserOptions": { + "ecmaVersion": 2017 + } +} diff --git a/test/es6/index.js b/test/es6/index.js index ce681c2..a41f493 100644 --- a/test/es6/index.js +++ b/test/es6/index.js @@ -17,6 +17,16 @@ module.exports = function() { assert.strictEqual(kindOf(promise), 'promise'); }); + it('should work for async functions', function() { + function awaiter() { + return true; + } + var gen = async function named() { + return await awaiter(); + }; + assert.equal(kindOf(gen), 'asyncfunction'); + }); + it('should work for generator functions', function() { var gen = function * named() { return true; @@ -73,18 +83,21 @@ module.exports = function() { var SetValuesIterator = new Set().values(); assert.equal(kindOf(SetValuesIterator), 'setiterator'); }); + it('should work for Map Iterator', function() { var MapValuesIterator = new Map().values(); assert.equal(kindOf(MapValuesIterator), 'mapiterator'); }); + it('should work for Array Iterator', function() { var ArrayEntriesIterator = [].entries(); assert.equal(kindOf(ArrayEntriesIterator), 'arrayiterator'); - }) + }); + it('should work for String Iterator', function() { var StringCharIterator = ''[Symbol.iterator](); assert.equal(kindOf(StringCharIterator), 'stringiterator'); - }) + }); it('should work for Symbol', function() { assert.equal(kindOf(Symbol('foo')), 'symbol'); @@ -135,5 +148,18 @@ module.exports = function() { var float64array = new Float64Array(); assert.equal(kindOf(float64array), 'float64array'); }); + + it('should work for ArrayBuffer', function() { + var arrayBuffer = new ArrayBuffer(24); + assert.equal(kindOf(arrayBuffer), 'arraybuffer'); + }); + + it('should work for DataView', function() { + var dataView = new DataView(new ArrayBuffer(24)); + assert.equal(kindOf(dataView), 'dataview'); + assert.equal(kindOf(dataView.buffer), 'arraybuffer'); + assert.equal(kindOf(dataView.byteLength), 'number'); + assert.equal(kindOf(dataView.byteOffset), 'number'); + }); }); }; From 9585acba2a2b53c0ff0d0cc9b7278d4d7c9c293e Mon Sep 17 00:00:00 2001 From: Pedro Batista Date: Wed, 26 Sep 2018 08:17:18 -0300 Subject: [PATCH 2/2] removed unnecessary entry for dataview --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index fb695a0..f122cd8 100644 --- a/index.js +++ b/index.js @@ -42,9 +42,6 @@ module.exports = function kindOf(val) { case 'Uint32Array': return 'uint32array'; case 'Float32Array': return 'float32array'; case 'Float64Array': return 'float64array'; - - // Data views - case 'DataView': return 'dataview'; } if (isGeneratorObj(val)) {