- 浏览: 399727 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
I am using the word, memoization, because it is cool and it sounds like a buzzword, and author of the javascript ninja also called it so.
so, what is memoization. " Memoization is the process of building a function which is capable of remembering its previously computed answers. "
so let 's see some sample implementation that realize the memoization.
/************************************** *@Summary * this is the js file that demonstrate the use of the memoization (memorization), I called it memoization as it is like some buzzword as Ninja is calling it so. * * Memorization is a very common technique that you can save some compute values as a cache, and you can shortcut some long operation if a value has already computed * * @Todo: * this file is not created, will come back to it with implementation ***************************************/ /** * @Summary * memoized * @Usage: * isPrime.memoized(5)... * isPrime._values[5] */ Function.prototype.memoized = function (key) { this._values = this._values || {}; // this is a common technique to initialize something that if it does not have a default value, assign one return this._values[key] !== undefined ? this._values[key] : this._values[key] = this.apply(this, arguments); // the reason why not to store this a fn and so some manipulation is because 'this' we are just using the same function }; /** * @Summary * memoize * @Usage: * var isPrime = (function(num) { .. }).memoize(); * isPrime(5) */ Function.prototype.memoize = function () { var fn = this; return function () { return fn.memoized.apply(fn, arguments); }; }; /** * @Summary * isPrime * @Comment: this is the function that we test for memoization */ function isPrime(num) { var prime = num != 1; for (var i = 2; i < num; i++) { if (num % i == 0) { prime = false; break; } } return prime; }
and the following code that test the memoizaion.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type ="text/javascript" src="memoization.js"></script> <script type="text/javascript" src="../unit.js"></script> <script type="text/javascript"> window.onload = function () { test("memoization test", function () { var isPrime_moized = isPrime.memoize(); assert(isPrime_moized(5), "Make sure the function works, 5 is prime."); assert(isPrime._values[5], "Make sure the answer is cached."); assert(isPrime.memoized(5), "Make sure the function works, 5 is prime."); assert(isPrime._values[5], "Make sure the answer is cached."); }); }; </script> <style type="text/css" > #results li.pass { color: Green } #results li.fail { color: Red } </style> </head> <body> <ul id="results" /> </body> </html>
It is believed that with the memoization, you can achieve faster process (trade off spaces for time.)
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 928the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2254Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1659In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 906In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 700Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 972Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2078In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1024We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 819For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 881Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 766There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 766All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 522though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 549when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 517When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 747while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 533IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1042there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1039expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1144it is not as simple as you thin ...
相关推荐
JavaScript Memoization 是一种优化技术,通过存储函数的中间结果,避免重复计算,从而提高性能。在JavaScript中,由于其动态类型和对象的特性,实现Memoization非常简单且高效。 在 Fibonacci 数列的例子中,原始...
在学习JavaScript面向对象编程时,Memoization是一种值得掌握的技术,它用于提高函数的执行效率。Memoization的核心思想是缓存函数的执行结果,以避免重复计算,特别是对于那些计算成本高或耗时长的函数,这种技术尤...
这个库的主要功能是提供JavaScript的备忘录化(memoization)功能,帮助优化代码性能。 **描述分析:** “memoizerific,快速,小,高效的javascript memoization lib到memoize js函数”表明memoizerific库具有轻量...
例如,使用异步调用、memoization 技术和重构函数等方法可以提高 JavaScript 的运行速度。 本文讨论了如何提升 JavaScript 的运行速度的方法,包括避免嵌套循环、使用异步调用、memoization 技术、重构函数和防止...
本文提供了两种解决 JavaScript 中递归问题的方法,包括 Memoization 技术和将递归算法转换为迭代算法。这些方法可以帮助开发者提高 JavaScript 的运行速度,解决递归问题,提高 Web 应用程序的性能。 知识点: 1....
这种特性使得函数可以封装私有变量,实现数据隐藏,同时为函数式编程提供了记忆化(memoization)等高级技术的基础。 纯函数是函数式编程的另一个关键要素。纯函数是指给定相同的输入,总是返回相同的输出,并且...
Javascript 中的记忆一个小示例,通过 javascript 展示了“记忆化”的全部内容。 记忆???!!!??? 嗯,它是一种保存/缓存与参数有关的函数结果的方法——在未来,这有助于防止使用相同的参数执行相同的函数,...
6.9使用缓存计算(Memoization)来提高应用程序性能 6.10使用匿名函数包装全局变量 第7章处理事件 7.0简介 7.1检测页面何时完成载入 7.2使用Event对象捕获鼠标点击事件的位置 7.3创建一个通用的、可...
3. 缓存和记忆化(Memoization):利用闭包的特性,可以实现函数缓存。当函数需要处理耗时计算时,可以将结果保存在闭包内部,当下次遇到相同的输入时,直接从缓存中返回结果,无需重新计算。 4. 高阶函数:闭包...
除了基础功能,Lodash还包含了一些高级特性,如延迟计算(lazy evaluation)和函数记忆(memoization),这些可以帮助优化性能,尤其是在处理大数据集时。例如,函数记忆能记住先前计算的结果,避免重复计算,提升...
使用迭代方式替代递归,采用 memoization 技术优化递归,斐波那契数列的递归算法优化。 4. 过多的 DOM 调用 在 Web 开发中,JavaScript 的一个很重要的作用就是对 DOM 进行操作。但是对 DOM 的操作是非常昂贵的,...
避免重复计算和过度渲染,使用函数记忆化(memoization)技术来存储先前计算的结果,可以减少不必要的计算。此外,避免使用过于复杂的嵌套循环,尽可能使用更简洁的数据结构和算法。 在DOM操作上,批量处理比单个...
Memoization Chapter 5. Inheritance Section 5.1. Pseudoclassical Section 5.2. Object Specifiers Section 5.3. Prototypal Section 5.4. Functional Section 5.5. Parts Chapter 6. Arrays Section 6.1...
2. **记忆化**:闭包可以用于缓存计算结果,提高性能,例如在函数式编程中的memoization技术。 3. **事件处理**:在事件监听器中,闭包可以保留事件触发时的上下文,确保回调函数能够正确操作相关变量。 4. **异步...
4. **缓存和 memoization**:不可变数据使得缓存计算结果变得更加简单,因为可以基于输入值的哈希来确定是否需要重新计算。 5. **调试**:由于每次修改都会生成新的数据结构,因此可以通过比较不同版本来追踪代码中...
3. **缓存结果**:对于重复计算的情况,可以使用记忆化(memoization)技术来存储之前计算过的值,避免重复计算。 4. **考虑尾递归优化**:在支持尾递归优化的JavaScript环境中(如使用`--harmony_tailcalls`标志的...
标题为“javascript 精粹笔记”的这份文档显然是一份针对JavaScript语言的学习和应用技巧的集合。JavaScript是一种广泛应用于Web前端开发的脚本语言,随着互联网技术的发展,JavaScript的应用越来越广泛,不仅限于...
JavaScript和TypeScript的轻量级,高效的元组和值对象实现。 快速提醒一下什么是元组(使用Python): ( 1 , 2 , 3 ) == ( 1 , 2 , 3 ) # → True 类似JavaScript版本如下所示: '[1,2,3]' === '[1,2,3]' ; // → ...