Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async functions + Tests for arraybuffer, dataview and async function #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/index.js
Expand Up @@ -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);
162 changes: 102 additions & 60 deletions 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)
63 changes: 60 additions & 3 deletions 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<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(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(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var toString = Object.prototype.toString;

module.exports = function kindOf(val) {
var kindOf = function(val) {
if (val === void 0) return 'undefined';
if (val === null) return 'null';

Expand Down Expand Up @@ -45,6 +45,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)) {
Expand All @@ -66,6 +69,17 @@ module.exports = function kindOf(val) {
return type.slice(8, -1).toLowerCase().replace(/\s/g, '');
};

kindOf.async = function async(value) {
return __awaiter(this, void 0, void 0, function() {
return __generator(this, function(accel) {
switch (accel.label) {
case 0: return [4, kindOf(value)];
case 1: return [2, accel.sent()];
}
});
});
};

function ctorName(val) {
return val.constructor ? val.constructor.name : null;
}
Expand Down Expand Up @@ -129,5 +143,48 @@ function isBuffer(val) {
return false;
}

module.exports = kindOf;

/**
* Promise polyfills (async function), compatible with ES3 and ES5
*/

/* eslint-disable */
var __awaiter = (this && this.__awaiter) || function(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator['throw'](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function(resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), 'throw': verb(1), 'return': verb(2) }, typeof Symbol === 'function' && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function(v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError('Generator is already executing.');
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y['return'] : op[0] ? y['throw'] || ((t = y['return']) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 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)
});
});
15 changes: 8 additions & 7 deletions index.js
Expand Up @@ -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';
Expand Down Expand Up @@ -93,10 +91,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'
Expand Down Expand Up @@ -127,3 +121,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';
}
5 changes: 5 additions & 0 deletions test/es6/.eslintrc.json
@@ -0,0 +1,5 @@
{
"parserOptions": {
"ecmaVersion": 2017
}
}