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

Use prototype based Recorder for performance boost. #12

Merged
merged 4 commits into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions lib/babel-assertion-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,13 @@ var babelgen = require('babel-generator');
var define = require('define-properties');
var toBeCaptured = require('./to-be-captured');
var toBeSkipped = require('./to-be-skipped');
var helperCode = [
'(function () {',
' var captured = [];',
' function _capt (value, espath) {',
' captured.push({value: value, espath: espath});',
' return value;',
' }',
' function _expr (value, args) {',
' var source = {',
' content: args.content,',
' filepath: args.filepath,',
' line: args.line',
' };',
' if (args.generator) {',
' source.generator = true;',
' }',
' if (args.async) {',
' source.async = true;',
' }',
' return {',
' powerAssertContext: {',
' value: value,',
' events: captured',
' },',
' source: source',
' };',
' }',
' return {',
' _capt: _capt,',
' _expr: _expr',
' };',
'});'
].join('\n');

var fs = require('fs');
var helperCode = '(' +
fs.readFileSync(require.resolve('./power-assert-recorder.js'), 'utf8')
.split('\n')
.slice(2)
.join('\n')
+ ')()';

function BabelAssertionVisitor (babel, matcher, options) {
this.babel = babel;
Expand Down Expand Up @@ -211,7 +184,7 @@ BabelAssertionVisitor.prototype.createNewRecorder = function (nodePath) {
var helperNameNode = this.getRecordHelperNameNode(nodePath);
var recorderIdent = nodePath.scope.generateUidIdentifier('rec');
define(recorderIdent, { _generatedByEspower: true });
var init = types.callExpression(helperNameNode, []);
var init = types.newExpression(helperNameNode, []);
define(init, { _generatedByEspower: true });
nodePath.scope.push({ id: recorderIdent, init: init });
return recorderIdent;
Expand Down
24 changes: 24 additions & 0 deletions lib/power-assert-recorder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
module.exports = /* intentional newline */
function () {
function PowerAssertRecorder() {
this.captured = [];
}

PowerAssertRecorder.prototype._capt = function _capt (value, espath) {
this.captured.push({value: value, espath: espath});
return value;
};

PowerAssertRecorder.prototype._expr = function _expr (value, source) {
return {
powerAssertContext: {
value: value,
events: this.captured
},
source: source
};
};

return PowerAssertRecorder;
}
10 changes: 5 additions & 5 deletions test/fixtures/ArrayExpression/expected-presets-es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt([_rec._capt(foo, 'arguments/0/elements/0'), _rec._capt(bar, 'arguments/0/elements/1')], 'arguments/0'), {
content: 'assert([foo, bar])',
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/ArrayExpression/expected.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var _powerAssertRecorder = function () { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt([_rec._capt(foo, 'arguments/0/elements/0'), _rec._capt(bar, 'arguments/0/elements/1')], 'arguments/0'), {
content: 'assert([foo, bar])',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder();

assert(function (v) {
return v + 1;
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/ArrowFunctionExpression/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var _powerAssertRecorder = function () { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder();

assert(v => v + 1);

Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/AssignmentExpression/expected-presets-es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder(),
_rec5 = _powerAssertRecorder(),
_rec6 = _powerAssertRecorder(),
_rec7 = _powerAssertRecorder(),
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder(),
_rec5 = new _powerAssertRecorder(),
_rec6 = new _powerAssertRecorder(),
_rec7 = new _powerAssertRecorder(),
_rec6$_capt,
_rec6$_capt2,
_rec7$_capt,
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/AssignmentExpression/expected.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var _powerAssertRecorder = function () { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder(),
_rec5 = _powerAssertRecorder(),
_rec6 = _powerAssertRecorder(),
_rec7 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder(),
_rec5 = new _powerAssertRecorder(),
_rec6 = new _powerAssertRecorder(),
_rec7 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt(counter += 1, 'arguments/0'), {
content: 'assert(counter += 1)',
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/AwaitExpression/expected-presets-stage-3.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; };
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

var myAsync = function () {
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(a) {
var _rec = _powerAssertRecorder();
var _rec = new _powerAssertRecorder();

return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
Expand Down
18 changes: 9 additions & 9 deletions test/fixtures/BinaryExpression/expected-presets-es2015.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder(),
_rec5 = _powerAssertRecorder(),
_rec6 = _powerAssertRecorder(),
_rec7 = _powerAssertRecorder(),
_rec8 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder(),
_rec5 = new _powerAssertRecorder(),
_rec6 = new _powerAssertRecorder(),
_rec7 = new _powerAssertRecorder(),
_rec8 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt(4 !== 4, 'arguments/0'), {
content: 'assert(4 !== 4)',
Expand Down
18 changes: 9 additions & 9 deletions test/fixtures/BinaryExpression/expected.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var _powerAssertRecorder = function () { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder(),
_rec5 = _powerAssertRecorder(),
_rec6 = _powerAssertRecorder(),
_rec7 = _powerAssertRecorder(),
_rec8 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder(),
_rec5 = new _powerAssertRecorder(),
_rec6 = new _powerAssertRecorder(),
_rec7 = new _powerAssertRecorder(),
_rec8 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt(4 !== 4, 'arguments/0'), {
content: 'assert(4 !== 4)',
Expand Down
26 changes: 13 additions & 13 deletions test/fixtures/CallExpression/expected-presets-es2015.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

var _powerAssertRecorder = function _powerAssertRecorder() { var captured = []; function _capt(value, espath) { captured.push({ value: value, espath: espath }); return value; } function _expr(value, args) { var source = { content: args.content, filepath: args.filepath, line: args.line }; if (args.generator) { source.generator = true; } if (args.async) { source.async = true; } return { powerAssertContext: { value: value, events: captured }, source: source }; } return { _capt: _capt, _expr: _expr }; },
_rec = _powerAssertRecorder(),
_rec2 = _powerAssertRecorder(),
_rec3 = _powerAssertRecorder(),
_rec4 = _powerAssertRecorder(),
_rec5 = _powerAssertRecorder(),
_rec6 = _powerAssertRecorder(),
_rec7 = _powerAssertRecorder(),
_rec8 = _powerAssertRecorder(),
_rec9 = _powerAssertRecorder(),
_rec10 = _powerAssertRecorder(),
_rec11 = _powerAssertRecorder(),
_rec12 = _powerAssertRecorder();
var _powerAssertRecorder = function () { function PowerAssertRecorder() { this.captured = []; } PowerAssertRecorder.prototype._capt = function _capt(value, espath) { this.captured.push({ value: value, espath: espath }); return value; }; PowerAssertRecorder.prototype._expr = function _expr(value, source) { return { powerAssertContext: { value: value, events: this.captured }, source: source }; }; return PowerAssertRecorder; }(),
_rec = new _powerAssertRecorder(),
_rec2 = new _powerAssertRecorder(),
_rec3 = new _powerAssertRecorder(),
_rec4 = new _powerAssertRecorder(),
_rec5 = new _powerAssertRecorder(),
_rec6 = new _powerAssertRecorder(),
_rec7 = new _powerAssertRecorder(),
_rec8 = new _powerAssertRecorder(),
_rec9 = new _powerAssertRecorder(),
_rec10 = new _powerAssertRecorder(),
_rec11 = new _powerAssertRecorder(),
_rec12 = new _powerAssertRecorder();

assert(_rec._expr(_rec._capt(func(), 'arguments/0'), {
content: 'assert(func())',
Expand Down
Loading