Skip to content

Instantly share code, notes, and snippets.

@avalanche1
Last active January 28, 2017 13:15
Show Gist options
  • Save avalanche1/49664e6bef8a30749f358ae6d43ed57a to your computer and use it in GitHub Desktop.
Save avalanche1/49664e6bef8a30749f358ae6d43ed57a to your computer and use it in GitHub Desktop.
type checking perf test (http://jsbench.github.io/#49664e6bef8a30749f358ae6d43ed57a) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>type checking perf test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
var checkFuncArgs, checkType, foo, gbThrowError;
checkType = function(argPosition, argValue, type, parentName) {
var throwErr;
throwErr = function() {
return gbThrowError("arg #" + argPosition + " sd be '" + type + "'", parentName);
};
switch (type) {
case 'any':
break;
case 'arr':
if (!Array.isArray(argValue)) {
return throwErr();
}
break;
case 'fun':
if (typeof argValue !== 'function') {
return throwErr();
}
break;
case 'num':
if (typeof argValue !== 'number') {
return throwErr();
}
break;
case 'obj':
if (!(typeof argValue === 'object' && !Array.isArray(argValue))) {
return throwErr();
}
break;
case 'str':
if (typeof argValue !== 'string') {
return throwErr();
}
break;
default:
return gbThrowError("H.check.type(): unsupported type: '" + type + "'", parentName);
}
};
checkFuncArgs = function(fArgs, typeArr, fName) {
var arg, i, j, len, ref, results;
fName = (ref = fArgs.callee.name) != null ? ref : fName;
if (fArgs.length !== typeArr.length) {
gbThrowError(fName + "() supplied " + fArgs.length + " arguments, while " + typeArr.length + " required", 'H.check.funcArgs');
}
results = [];
for (i = j = 0, len = fArgs.length; j < len; i = ++j) {
arg = fArgs[i];
results.push(checkType(i + 1, arg, typeArr[i], fName));
}
return results;
};
gbThrowError = function(description, funcName) {
if (funcName) {
description = funcName + "(): " + description;
}
if (typeof Meteor !== "undefined" && Meteor !== null) {
if (Meteor.isDev || Meteor.isServer) {
console.trace();
throw new Meteor.Error(description);
} else {
throw new Meteor.Error("You didn't say the magic word!", "AH! AH! AH!");
}
} else {
throw Error(description);
}
};
// foo = function(a, b, c) {
// checkFuncArgs(arguments, ['arr', 'fun', 'obj']);
// return 'ok';
// };
};
suite.add("foo = function(a, b, c) {", function () {
foo = function(a, b, c) {
checkFuncArgs(arguments, ['arr', 'fun', 'obj']);
return 'ok';
};
foo([1, 2, 3], (function() {
return 'bar';
}), {
foo: 'bar'
});
});
suite.add("foo = function(a, b, c) {", function () {
foo = function(a, b, c) {
return 'ok';
};
foo([1, 2, 3], (function() {
return 'bar';
}), {
foo: 'bar'
});
});
suite.add("foo = function(a, b, c) {", function () {
foo = function(a, b, c) {
checkType(0, arguments[0],'arr', 'parentName')
checkType(1, arguments[1],'fun', 'parentName')
checkType(2, arguments[2],'obj', 'parentName')
return 'ok';
};
foo([1, 2, 3], (function() {
return 'bar';
}), {
foo: 'bar'
});
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("type checking perf test");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment