Skip to content

Commit

Permalink
Optimized the most common use-case likely with Topiarist (verifying a…
Browse files Browse the repository at this point in the history
…rguments using isA()) so that it's now ~2.5 times slower than doing the same check manually, rather than being ~25 times slower, as was the case before. To achive this I did have to relax the argument checking performed, but this actuallly resulted in the browser throwing a clearer error message than what Topiarist was producing anyway.
  • Loading branch information
dchambers committed Oct 25, 2014
1 parent 03e51ba commit aeb35a1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions lib/topiarist.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,17 @@
* or is null, false otherwise.
*/
function isA(instance, parent) {
if(instance == null) {
return false;
}

// sneaky edge case where we're checking against an object literal we've mixed in or against a prototype of
// something.
if (typeof parent === 'object' && parent.hasOwnProperty('constructor')) {
parent = parent.constructor;
}

assertArgumentOfType('function', parent, ERROR_MESSAGES.NOT_CONSTRUCTOR, 'Parent', 'isA');

if (instance == null) {
return false;
}

if (instance instanceof parent) {
if((instance.constructor === parent) || (instance instanceof parent)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "topiarist",
"version": "0.1.0",
"version": "0.1.1",
"author": "kybernetikos <[email protected]>",
"description": "Topiarist provides tree and shape-based type verification for JavaScript.",
"main": "./lib/topiarist.js",
Expand Down
4 changes: 2 additions & 2 deletions spec/IsASpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("topiarist.isA", function() {
it('throws an error if the potential assignee is not a constructor.', function() {
expect( function() {
topiarist.isA(instance, 34);
}).toThrow(err.NOT_CONSTRUCTOR('Parent', 'isA', 'number'));
}).toThrow();
});

it('returns false for a null instance.', function() {
Expand Down Expand Up @@ -85,4 +85,4 @@ describe("topiarist.isA", function() {
it('returns true for an instance and a mixin that was defined without a constructor.', function() {
expect( topiarist.isA(instance, ObjMixin)).toBe( true );
});
});
});

0 comments on commit aeb35a1

Please sign in to comment.