From e889aff281b3fb1a55038ea1bede19cae5b8b99e Mon Sep 17 00:00:00 2001 From: Jonathan Sterling Date: Tue, 8 Sep 2015 12:01:36 -0700 Subject: [PATCH] Memoize feature detection (resolve #5) --- src/DOM/BrowserFeatures/Detectors.js | 11 ++++++++++- test/Main.purs | 13 ++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/DOM/BrowserFeatures/Detectors.js b/src/DOM/BrowserFeatures/Detectors.js index dbf6330..649ad18 100644 --- a/src/DOM/BrowserFeatures/Detectors.js +++ b/src/DOM/BrowserFeatures/Detectors.js @@ -1,7 +1,13 @@ // module DOM.BrowserFeatures.Detectors +var _inputTypeSupportMemoTable = {}; + exports._detectInputTypeSupport = function(type) { return function() { + if (_inputTypeSupportMemoTable.hasOwnProperty(type)) { + return _inputTypeSupportMemoTable[type]; + } + var el = document.createElement("input"); try { @@ -10,6 +16,9 @@ exports._detectInputTypeSupport = function(type) { return false; } - return el.type === type; + var result = el.type === type; + _inputTypeSupportMemoTable[type] = result; + + return result; }; }; diff --git a/test/Main.purs b/test/Main.purs index 5ca0ff4..c0d6d42 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -11,6 +11,13 @@ import DOM.BrowserFeatures.Detectors main :: Eff (dom :: DOM, console :: CONSOLE) Unit main = do - features <- detectBrowserFeatures - void $ for IT.allInputTypes \ty -> - log $ show ty ++ if features.inputTypeSupported ty then " supported" else " not supported" + queryFeatureSupport + log "Will query feature support again, using memoized results" + queryFeatureSupport + log "Done" + + where + queryFeatureSupport = do + features <- detectBrowserFeatures + void $ for IT.allInputTypes \ty -> + log $ show ty ++ if features.inputTypeSupported ty then " supported" else " not supported"