Caching Sin Values (v2)

Revision 2 of this benchmark created by Mike on


Description

A check to see if there is any increase in speed by using a function that caches values as they are computed.

Preparation HTML

<script type="text/javascript">
    var cachedSin = (function() {  
        var cache = [],            
        sin = Math.sin; /* Faster than global... */   
 
        return function(n) {
            return (cache[n]) ? cache[n] : (cache[n] = sin(n));
        } 
    })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
cachedSin()
/* Notes: 
       - The loops here are to ensure values are repeated and cached.
       - Tests should be run several times since the cache needs to be built up first.
*/
for(var i = 0; i < 2; ++i) {
    for(var j = 0; j < 20; j += 3)        
        var x = cachedSin(j);
}
ready
Math.sin()
for(var i = 0; i < 2; ++i) {
    for(var j = 0; j < 20; j += 3)        
        var x = Math.sin(j);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 2: published by Mike on