From 2958a792a4f3e1e0a5363b62b8888dca87b5670a Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Fri, 2 Nov 2018 15:19:35 +0000 Subject: [PATCH] Refactor is* functions --- package.json | 2 +- src/index.js | 34 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index a3c002a..be8cd44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trimkit", - "version": "1.0.2", + "version": "1.0.3", "description": "Javascript and DOM abstractions for smaller minifiable code", "main": "dist/trimkit.js", "module": "dist/trimkit.js", diff --git a/src/index.js b/src/index.js index a44ec11..5953ae3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Timothy Farrell +// Copyright (c) 2018 Timothy Farrell // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -48,29 +48,31 @@ export const ObjectKeys = Object.keys; // Size saved after: ceil(20 / 7) = 3 usages export const DOMDocument = self.document; -// ES5 output: function a(b){return"function"===typeof b}; +const isType = str => obj => typeof obj === 'function'; + +// ES5 output A: var c=(function(t){return function a(b){return t===typeof b}})('function'); +// ES5 output B: var c=a('function'); // Invocation comparison: typeof a==='function' ~~~ a(b) // Size delta per invocation: 21 - 4 = 17 bytes -// Size saved after: ceil(43 / 17) = 3 usages -export function isFunction(obj) { - return typeof obj === 'function'; -} +// Size saved after (A): ceil(75 / 17) = 5 usages +// Size saved after (B): ceil(20 / 17) = 2 usages +export const isFunction = isType('function'); -// ES5 output: function a(b){return"string"===typeof b}; +// ES5 output A: var c=(function(t){return function a(b){return t===typeof b}})('string'); +// ES5 output B: var c=a('string'); // Invocation comparison: typeof a==='string' ~~~ a(b) // Size delta per invocation: 19 - 4 = 15 bytes -// Size saved after: ceil(41 / 15) = 3 usages -export function isString(obj) { - return typeof obj === 'string'; -} +// Size saved after (A): ceil(73 / 15) = 5 usages +// Size saved after (B): ceil(18 / 15) = 2 usages +export const isString = isType('string'); -// ES5 output: function a(b){return"number"===typeof b}; +// ES5 output A: var c=(function(t){return function a(b){return t===typeof b}})('number'); +// ES5 output B: var c=a('number'); // Invocation comparison: typeof a==='number' ~~~ a(b) // Size delta per invocation: 19 - 4 = 15 bytes -// Size saved after: ceil(41 / 15) = 3 usages -export function isNumber(obj) { - return typeof obj === 'number'; -} +// Size saved after (A): ceil(73 / 15) = 5 usages +// Size saved after (B): ceil(18 / 15) = 2 usages +export const isNumber = isType('number'); // ES5 declaration: var a=null; // Invocation comparison: null ~~~ a