From aeb35a15e8bba29376ff3843a6236b505cf38331 Mon Sep 17 00:00:00 2001 From: Dominic Chambers Date: Sat, 25 Oct 2014 21:44:31 +0100 Subject: [PATCH] Optimized the most common use-case likely with Topiarist (verifying arguments 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. --- lib/topiarist.js | 12 +++++------- package.json | 2 +- spec/IsASpec.js | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/topiarist.js b/lib/topiarist.js index 7318365..3924157 100644 --- a/lib/topiarist.js +++ b/lib/topiarist.js @@ -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; } diff --git a/package.json b/package.json index 6d4f1a8..d47a898 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "topiarist", - "version": "0.1.0", + "version": "0.1.1", "author": "kybernetikos ", "description": "Topiarist provides tree and shape-based type verification for JavaScript.", "main": "./lib/topiarist.js", diff --git a/spec/IsASpec.js b/spec/IsASpec.js index ff11fcf..495175b 100644 --- a/spec/IsASpec.js +++ b/spec/IsASpec.js @@ -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() { @@ -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 ); }); -}); \ No newline at end of file +});