Can JavaScript memoization only be used to cache returned results?

kaidez

I've created the function below, which will run on every page of my site. It runs fast enough but as it runs on every page, it seems to be a good candidate for being cached, maybe using memoization. In my research on the subject, memoization always caches the results that are being sent out with a return statement. This makes perfect sense but as my function isn't returning anything, I'm wondering if it can still be cached? Using something other than memoization perhaps?

Here's my function:

// A requirejs module
define(function() {

 var loadMenu,
   cssdisabled,
   testcss,
   currstyle; 

 /*  
  *  Dynamically create a form that looks like this:
  *
  *  <form action="/search.html" id="js-searchbox" class="form">
  *    <input type="text" name="q" id="tipue_search_input"
  *     placeholder="Search...">
  *    <input type="submit" id="tipue_search_button" value="Search">
  *  </form>
  */
 loadMenu = function() {
   var loadBox = document.getElementById("searchbox"),
     footerBox = document.getElementById("search-about-column"),
     frag = document.createDocumentFragment(),
     form = document.createElement("form"),
     searchTextBox = document.createElement("input"),
     searchButton = document.createElement("input");

 // set attributes for form
 form.action = "/search.html";
 form.setAttribute("role", "search");
 form.id = "js-searchbox";
 form.setAttribute("class", "form");

 // set attributes for Search text box
 searchTextBox.type = "text";
 searchTextBox.name = "q";
 searchTextBox.id = "tipue_search_input";
 searchTextBox.placeholder = "Search...";

 // set attributes for Submit button
 searchButton.type = "submit";
 searchButton.setAttribute("class", "btnSearch");
 searchButton.value = "Search";

 // Arrange elements
 form.appendChild(searchTextBox);
 form.appendChild(searchButton);

 // Load arranged elements into document fragment
 frag.appendChild(form);

 // Load document fragment into #searchbox, which is already on the page
 loadBox.appendChild(frag);
}

 cssdisabled = false;

 testcss = document.createElement('div');

 testcss.style.position = 'absolute';

 document.getElementsByTagName('body')[0].appendChild(testcss);

 if (testcss.currentStyle) {
   currstyle = testcss.currentStyle['position'];
 }

 else if (window.getComputedStyle) {
  currstyle = document.defaultView.getComputedStyle(testcss, null).getPropertyValue('position');
} 

 cssdisabled = (currstyle === 'static') ? true : false;

 document.getElementsByTagName('body')[0].removeChild(testcss);

 if (cssdisabled === false) {
   loadMenu();
 } else {
   return false;
  }

 });
Paul Sweatte

Use the frag as your memo:

frag.appendChild(form);
loadMenu.frag = frag;

then update the assignment:

loadMenu = loadMenu.frag || function() {...}

References

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Doctrine 2.0 query cache results can't be used

分類Dev

PerRequestLifetimeManager can only be used in the context of an HTTP request

分類Dev

Can return only be used once in a block of code?

分類Dev

Can FireBase offline mode can be used for localstorage only?

分類Dev

Cabal: What does "only already installed instances can be used" mean?

分類Dev

Writing a Python class that can only be used as a context manager

分類Dev

What is the way to ensure that the RESTful API can only be used by the SPA application?

分類Dev

How can I output iperf results for only Interval and Bandwidth?

分類Dev

ping: show only results

分類Dev

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

分類Dev

Can I turn off two-factor authentication for an account that is used only in build machine?

分類Dev

Declare property of protocol shows Protocol can only be used as a generic constraint because it has Self or associated type requirements

分類Dev

How to use a Regex for a list of options, but each option can only be used once?

分類Dev

Java - Write a condition for a classes method that ensures only some items of a HashMap can be used.

分類Dev

Cannot query cache by affinity key when custom cache template is used

分類Dev

How to cache intermediate results when rendering with parameters?

分類Dev

How can I show only a section of a video frame with CSS or JavaScript?

分類Dev

How can I show only a section of a video frame with CSS or JavaScript?

分類Dev

Only one item being returned from array

分類Dev

Can '->to()' be used in Mojolicious::Lite?

分類Dev

can IN be used on a sql ON clause

分類Dev

Two results are printed when headers are used in golang

分類Dev

Ruby: Call a method on returned object to detect if wrapper was used

分類Dev

javascript least amount of elements from an integer array that can be used to get to a total value

分類Dev

Memoization Function In Kotlin

分類Dev

Immutable Memoization - is it possible?

分類Dev

Fast memoization with tuples

分類Dev

Memoization of a Recursive Search

分類Dev

Javascript cache http get result

Related 関連記事

  1. 1

    Doctrine 2.0 query cache results can't be used

  2. 2

    PerRequestLifetimeManager can only be used in the context of an HTTP request

  3. 3

    Can return only be used once in a block of code?

  4. 4

    Can FireBase offline mode can be used for localstorage only?

  5. 5

    Cabal: What does "only already installed instances can be used" mean?

  6. 6

    Writing a Python class that can only be used as a context manager

  7. 7

    What is the way to ensure that the RESTful API can only be used by the SPA application?

  8. 8

    How can I output iperf results for only Interval and Bandwidth?

  9. 9

    ping: show only results

  10. 10

    Protocol can only be used as a generic constraint because it has Self or associatedType requirements

  11. 11

    Can I turn off two-factor authentication for an account that is used only in build machine?

  12. 12

    Declare property of protocol shows Protocol can only be used as a generic constraint because it has Self or associated type requirements

  13. 13

    How to use a Regex for a list of options, but each option can only be used once?

  14. 14

    Java - Write a condition for a classes method that ensures only some items of a HashMap can be used.

  15. 15

    Cannot query cache by affinity key when custom cache template is used

  16. 16

    How to cache intermediate results when rendering with parameters?

  17. 17

    How can I show only a section of a video frame with CSS or JavaScript?

  18. 18

    How can I show only a section of a video frame with CSS or JavaScript?

  19. 19

    Only one item being returned from array

  20. 20

    Can '->to()' be used in Mojolicious::Lite?

  21. 21

    can IN be used on a sql ON clause

  22. 22

    Two results are printed when headers are used in golang

  23. 23

    Ruby: Call a method on returned object to detect if wrapper was used

  24. 24

    javascript least amount of elements from an integer array that can be used to get to a total value

  25. 25

    Memoization Function In Kotlin

  26. 26

    Immutable Memoization - is it possible?

  27. 27

    Fast memoization with tuples

  28. 28

    Memoization of a Recursive Search

  29. 29

    Javascript cache http get result

ホットタグ

アーカイブ