Browse Source

EAGLESIX-3880: add test, arrayExtend example, jscs, styling

Kyle P Davis 10 years ago
parent
commit
226b527995

+ 45 - 0
.jscsrc

@@ -0,0 +1,45 @@
+{
+  "disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"],
+  "disallowKeywords": ["with"],
+  "disallowKeywordsOnNewLine": ["else", "catch", "finally"],
+  "disallowMixedSpacesAndTabs": true,
+  "disallowNewlineBeforeBlockStatements": true,
+  "disallowQuotedKeysInObjects": true,
+  "disallowSpaceAfterObjectKeys": true,
+  "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"],
+  "disallowSpaceBeforeBinaryOperators": [","],
+  "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
+  "disallowSpacesInFunctionExpression": {
+  	"beforeOpeningRoundBrace": true
+  },
+  "disallowSpacesInsideArrayBrackets": true,
+  "disallowSpacesInsideObjectBrackets": true,
+  "disallowSpacesInsideParentheses": true,
+  "disallowTrailingWhitespace": true,
+  "disallowYodaConditions": true,
+  "requireBlocksOnNewline": true,
+  "requireCurlyBraces": ["for", "while", "do", "try", "catch"],
+  "requireLineFeedAtFileEnd": true,
+  "requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="],
+  "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
+  "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="],
+  "requireSpacesInConditionalExpression": {
+  	"afterTest": true,
+  	"beforeConsequent": true,
+  	"afterConsequent": true,
+  	"beforeAlternate": true
+  },
+  "requireSpacesInFunctionExpression": {
+  	"beforeOpeningCurlyBrace": true
+  },
+  "requireTrailingComma": {
+  	"ignoreSingleLine": true,
+  	"ignoreSingleValue": true
+  },
+  "safeContextKeyword": ["self"],
+  "validateJSDoc": {
+	"checkParamNames": true,
+	"checkRedundantParams": true,
+	"requireParamTypes": true
+  }
+}

+ 8 - 4
README.md

@@ -29,14 +29,18 @@ benchmarksman bench/array.js
 
 ### Programmatic
 ```javascript
-exports.Date = {
-	"#getTime()": function(){
+exports.dateGetTime = {
+
+	"#getTime()": function() {
 		new Date().getTime();
 	},
-	".now()": function(){
+
+	".now()": function() {
 		Date.now();
-	}
+	},
+
 };
+
 if (!module.main) require("benchmarksman").runner(exports);
 ```
 

+ 4 - 2
example/arrayClear.js

@@ -16,11 +16,13 @@ exports.arrayClear = {
 	},
 
 	"while length, pop": function() {
-		while (this.arr.length > 0) this.arr.pop();
+		while (this.arr.length > 0) {
+			this.arr.pop();
+		}
 	},
 
 };
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 61 - 0
example/arrayExtend.js

@@ -0,0 +1,61 @@
+"use strict";
+// benchmark ways to clear an Array
+
+
+// tests
+exports.arrayExtend = {
+
+	beforeEach: function() {
+
+		var N = 1000;
+		this.MAX = N * 1000;
+
+		this.arr = [];
+
+		this.data = Array.apply(0, new Array(this.N)).map(function(v, i) {
+			return i;
+		});
+
+		Array.prototype.extendForEachPush = function extendForEachPush(items) {
+			items.forEach(function(x) {
+				this.push(x);
+			}, this);
+		};
+
+		Array.prototype.extendForAssign = function extendForAssign(items) {
+			for (var ii = this.length, i = 0, l = items.length; i < l; i++) {
+				this[ii++] = items[i];
+			}
+		};
+
+		Array.prototype.extendPushApply = function extendPushApply(items) {
+			this.push.apply(this, items);
+		};
+
+	},
+
+	"assign old.concat(new)": function() {
+		this.arr = this.arr.concat(this.data);
+		if (this.arr.length > this.MAX) this.arr = [];
+	},
+
+	"forEach push": function() {
+		this.arr.extendForEachPush(this.data);
+		if (this.arr.length > this.MAX) this.arr = [];
+	},
+
+	"for assign": function() {
+		this.arr.extendForAssign(this.data);
+		if (this.arr.length > this.MAX) this.arr = [];
+	},
+
+	"push apply": function() {
+		this.arr.extendPushApply(this.data);
+		if (this.arr.length > this.MAX) this.arr = [];
+	},
+
+};
+
+
+// if run directly run benchmarks
+if (!module.main) require("benchmarksman").runner(exports);

+ 3 - 3
example/arrayForEach.js

@@ -12,13 +12,13 @@ var noop = function() {},
 // tests
 exports.arrayForEach = {
 
-	"for": function() {
+	"for loop": function() {
 		for (var i = 0, l = arr.length; i < l; i++) {
 			noop(arr[i]);
 		}
 	},
 
-	"forEach": function() {
+	"forEach loop": function() {
 		arr.forEach(noop);
 	},
 
@@ -26,4 +26,4 @@ exports.arrayForEach = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 6 - 6
example/arrayIndexOf.js

@@ -5,9 +5,9 @@
 exports.arrayIndexOf = {
 
 	// setup
-	"before": function () {
+	before: function() {
 
-		this.arr = Array.apply(0, Array(1000)).map(function(v, i) {
+		this.arr = Array.apply(0, new Array(1000)).map(function(v, i) {
 			return i;
 		});
 
@@ -21,15 +21,15 @@ exports.arrayIndexOf = {
 
 	},
 
-	"fn native": function () {
+	"fn native": function() {
 		this.arr.indexOf(this.arr.length - 1);
 	},
 
-	"fn for loop": function () {
+	"fn for loop": function() {
 		this.getIndexOf(this.arr, this.arr.length - 1);
 	},
 
-	"inline for loop": function () {
+	"inline for loop": function() {
 		var arr = this.arr,
 		item = this.arr.length - 1;
 		for (var i = 0, l = arr.length; i < l; i++) {
@@ -41,4 +41,4 @@ exports.arrayIndexOf = {
 };
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 8 - 5
example/dateGetTime.js

@@ -3,15 +3,18 @@
 
 
 // tests
-exports.Date = {
-	"#getTime()": function(){
+exports.dateGetTime = {
+
+	"#getTime()": function() {
 		new Date().getTime();
 	},
-	".now()": function(){
+
+	".now()": function() {
 		Date.now();
-	}
+	},
+
 };
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 1 - 1
example/jsonParse.js

@@ -29,4 +29,4 @@ exports.jsonParse = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 7 - 7
example/knownSetHas.js

@@ -11,23 +11,23 @@ var name = "things",
 // tests
 exports.knownSetHas = {
 
-	"array indexOf closure": function() {
+	"array indexOf, ref": function() {
 		return arr.indexOf(name) !== -1;
 	},
 
-	"array indexOf inline": function() {
+	"array indexOf, inline": function() {
 		return ["foo", "bar", "baz", "qux", "stuff", "things"].indexOf(name) !== -1;
 	},
 
-	"object in closure": function() {
+	"object in, closure": function() {
 		return name in obj;
 	},
 
-	"object in inline": function() {
+	"object in, inline": function() {
 		return name in {foo:"foo", bar:"bar", baz:"baz", qux:"qux", stuff:"stuff", things:"things"};
 	},
 
-	"switch": function() {
+	"switch, inline": function() {
 		switch (name) {
 			case "foo":
 			case "bar":
@@ -41,7 +41,7 @@ exports.knownSetHas = {
 		}
 	},
 
-	"if": function() {
+	"if, inline": function() {
 		return name === "foo" ||
 			name === "bar" ||
 			name === "baz" ||
@@ -54,4 +54,4 @@ exports.knownSetHas = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 3 - 3
example/maths.js

@@ -1,12 +1,12 @@
 "use strict";
 
-exports.Maths = {
+exports.maths = {
 
-	"multiply": function() {
+	"native multiply": function() {
 		return 2 + 2;
 	},
 
-	"divide": function() {
+	"native divide": function() {
 		return 42 / 11;
 	},
 

+ 7 - 4
example/objectHasKeys.js

@@ -6,8 +6,9 @@
 var obj = {foo:"foo", bar:"bar", baz:"baz", qux:"qux", stuff:"stuff", things:"things"};
 
 function hasFirstKey(o) {
-	for (var k in o) //jshint ignore:line
+	for (var k in o) { //jshint ignore:line
 		return true;
+	}
 	return false;
 }
 
@@ -31,15 +32,17 @@ exports.objectHasKeys = {
 	},
 
 	"inline: for k in obj, if hasOwnProperty, return": function() {
-		for (var k in obj) //jshint ignore:line
+		for (var k in obj) {
 			if (obj.hasOwnProperty(k))
 				return true;
+		}
 		return false;
 	},
 
 	"inline: for k in obj, return": function() {
-		for (var k in obj) //jshint ignore:line
+		for (var k in obj) { //jshint ignore:line
 			return true;
+		}
 		return false;
 	},
 
@@ -51,4 +54,4 @@ exports.objectHasKeys = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 1 - 1
example/typeArray.js

@@ -21,4 +21,4 @@ exports.typeArray = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 1 - 1
example/typeIsExact.js

@@ -30,4 +30,4 @@ exports.typeIsExact = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 1 - 1
example/typeObject.js

@@ -26,4 +26,4 @@ exports.typeObject = {
 
 
 // if run directly run benchmarks
-if (!module.main) return require("benchmarksman").runner(exports);
+if (!module.main) require("benchmarksman").runner(exports);

+ 2 - 2
lib/reporter.js

@@ -7,10 +7,10 @@
  * @param {Benchmark} bench  The `benchmark.js` test
  */
 module.exports = function reporter(bench) {
-	var ctx = this;
+	var self = this;
 	return bench
 		.on("error", function() {
-			if (ctx.file) console.error("ERROR in file: " + ctx.file);
+			if (self.file) console.error("ERROR in file: " + self.file);
 			throw bench.error;
 		})
 		.on("complete", function() {

+ 1 - 1
lib/runner.js

@@ -19,7 +19,7 @@ module.exports = function runner(tests) { //jshint maxcomplexity:19
 	if (tests.before instanceof Function) tests.before.call(ctx);
 	for (var name in tests) { //jshint ignore:line
 		if (!tests.hasOwnProperty(name)) continue;
-		if (~["before", "after", "beforeEach", "afterEach"].indexOf(name)) continue;
+		if (["before", "after", "beforeEach", "afterEach"].indexOf(name) !== -1) continue;
 		var test = tests[name],
 			fullName = ctx.fullName ? ctx.fullName + " " + name : name;
 		if (test instanceof Function) {

+ 1 - 1
main.js

@@ -10,7 +10,7 @@ var lib = require("./lib/"),
 module.exports = function main() {
 	var pkg = require("./package.json"),
 		args = process.argv.slice(2);
-	if (args.length === 0 || ~["-h", "--help"].indexOf(args[0])) {
+	if (args.length === 0 || ["-h", "--help"].indexOf(args[0]) !== -1) {
 		console.error("USAGE: %s [bench/slow.js]", pkg.name);
 	}
 	args.forEach(runner);

+ 6 - 1
package.json

@@ -7,7 +7,10 @@
     "benchmarksman": "benchmarksman.js"
   },
   "scripts": {
-    "test": "mocha test/{,**/}*_test.js",
+    "test": "eval 'set -e' ';npm run '{mocha,jscs,jshint}",
+    "mocha": "mocha test/{,**/}*_test.js",
+    "jshint": "jshint -e js,json .",
+    "jscs": "jscs *.js lib/ test/ example/",
     "benchmarks": "./benchmarksman.js example/*.js",
     "browserify": "browserify --standalone benchmarksman ./lib/index.js > benchmarksman.web.js",
     "webpack": "webpack -v -c --progress -d --output-library-target var --output-library benchmarksman --entry=./lib/ benchmarksman.web.js"
@@ -18,6 +21,8 @@
     "benchmark": "^1.0.0"
   },
   "devDependencies": {
+    "jshint": "^2.5.10",
+    "jscs": "^1.8.1",
     "microtime": "^1.0.1",
     "mocha": "^1.21.5"
   }

+ 43 - 0
test/benchmarksman_test.js

@@ -0,0 +1,43 @@
+var assert = require("assert"),
+	benchmarksman = require("../");
+
+exports.benchmarksman = {
+
+	"should run benchmarks": function(done) {
+		this.timeout(0);
+		var benchmarks = {
+				"do nothing": function() {
+					null;
+				},
+				"do maths": function() {
+					1 + 2 - 3 / 4 * 5 % 6;
+				},
+			},
+			benchmarksKeys = Object.keys(benchmarks),
+			benchmarksCount = benchmarksKeys.length,
+			benchmarked = {},
+			benchmarkedKeys = [],
+			benchmarkedCount = 0,
+			opts = {
+				reporter: function(bench) {
+					benchmarksman.reporter.apply(this, arguments);
+					bench.on("complete", function() {
+						benchmarked[bench.name] = bench;
+						benchmarkedKeys.push(bench.name);
+						benchmarkedCount++;
+						if (benchmarkedCount === benchmarksCount){
+							assert.deepEqual(benchmarkedKeys, benchmarksKeys);
+							assert(benchmarked["do nothing"].hz > benchmarked["do maths"].hz,
+								"doing nothing should be faster than doing maths");
+							return done();
+						}
+					});
+				}
+			};
+		benchmarksman.runner.call(opts, benchmarks);
+	},
+
+};
+
+// Mocha one-liner to make these tests self-hosted
+if (!module.main) (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);

+ 2 - 1
test/mocha.opts

@@ -1,3 +1,4 @@
 --reporter spec
---ui bdd
+--ui exports
 --recursive
+test/{,**/}*_test.js