Skip to content

Instantly share code, notes, and snippets.

@skaarlcooper
Forked from bonza-labs/caching_wrapper.js
Created May 13, 2024 16:25
Show Gist options
  • Save skaarlcooper/f965bf64d9c741446f78bf9bb47c8025 to your computer and use it in GitHub Desktop.
Save skaarlcooper/f965bf64d9c741446f78bf9bb47c8025 to your computer and use it in GitHub Desktop.

Revisions

  1. @craigbeck craigbeck revised this gist Sep 16, 2011. 1 changed file with 45 additions and 8 deletions.
    53 changes: 45 additions & 8 deletions caching_wrapper.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,29 @@

    var $w = function(string) { return string.split(' '); };

    var asInts = function(arr) {
    var results=[];
    for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10));
    return results;
    };

    Array.prototype.map = function(func) {
    var results=[];
    for(i=0, m=this.length; i<m; i++) results.push(func(this[i]));
    return results;
    };

    var time = function(subject, label) {
    this.seq = 1;
    var name = label || this.seq++;
    console.time(name);
    subject();
    console.timeEnd(name);
    }

    var primes = asInts($w('524287 9369319 2147483647'));
    var notprimes = asInts($w('4 6 8 15 21 524284 9369312'));

    function isPrime(num) {
    if (!num) return false;
    var prime = num != 1;
    @@ -10,11 +36,22 @@ function isPrime(num) {
    return prime;
    }

    Function.prototype.cached = function(o) {
    if (!this.cache) this.cache = {};
    if (this.cache[o] == null) {
    console.log('cache miss');
    this.cache[o] = this(o);
    } else { console.log('cache hit'); }
    return this.cache[o];
    };
    Function.prototype.cached = function() {
    var self = this;
    if (!self.cache) self.cache = {};

    return function(o) {
    if (self.cache[o] == null) {
    console.log('cache miss');
    self.cache[o] = self(o);
    } else { console.log('cache hit'); }
    return self.cache[o];
    }
    };

    isPrimeCached = isPrime.cached();


    time(function(){ primes.map(isPrime); }, 'isprime-no-cache');
    time(function(){ primes.map(isPrimeCached); }, 'isprime-with-cache first');
    time(function(){ primes.map(isPrimeCached); }, 'isprime-with-cache repeated');
  2. @craigbeck craigbeck renamed this gist Sep 16, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @craigbeck craigbeck revised this gist Sep 16, 2011. 1 changed file with 19 additions and 40 deletions.
    59 changes: 19 additions & 40 deletions caching_wrapper.html
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,20 @@
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    var $w = function(string) { return string.split(' '); };
    var asInts = function(arr) {
    var results=[];
    for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10));
    return results;
    };

    var someprimes = asInts($w('524287 9369319 2147483647'));
    function isPrime(num) {
    if (!num) return false;
    var prime = num != 1;
    for ( var i = 2; i < num; i++ ) {
    if ( num % i == 0 ) {
    prime = false;
    break;
    }
    }
    return prime;
    }

    function isPrime(num) {
    if (!num) return false;
    var prime = num != 1;
    for ( var i = 2; i < num; i++ ) {
    if ( num % i == 0 ) {
    prime = false;
    break;
    }
    }
    return prime;
    }

    Function.prototype.cached = function(o) {
    if (!this.cache) this.cache = {};
    if (this.cache[o] == null) {
    console.log('cache miss');
    this.cache[o] = this(o);
    } else { console.log('cache hit'); }
    return this.cache[o];
    };


    </script>
    </head>
    <body>

    </body>
    </html>
    Function.prototype.cached = function(o) {
    if (!this.cache) this.cache = {};
    if (this.cache[o] == null) {
    console.log('cache miss');
    this.cache[o] = this(o);
    } else { console.log('cache hit'); }
    return this.cache[o];
    };
  4. @craigbeck craigbeck created this gist Sep 15, 2011.
    41 changes: 41 additions & 0 deletions caching_wrapper.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    var $w = function(string) { return string.split(' '); };
    var asInts = function(arr) {
    var results=[];
    for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10));
    return results;
    };

    var someprimes = asInts($w('524287 9369319 2147483647'));

    function isPrime(num) {
    if (!num) return false;
    var prime = num != 1;
    for ( var i = 2; i < num; i++ ) {
    if ( num % i == 0 ) {
    prime = false;
    break;
    }
    }
    return prime;
    }

    Function.prototype.cached = function(o) {
    if (!this.cache) this.cache = {};
    if (this.cache[o] == null) {
    console.log('cache miss');
    this.cache[o] = this(o);
    } else { console.log('cache hit'); }
    return this.cache[o];
    };


    </script>
    </head>
    <body>

    </body>
    </html>