Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let car = {
  2.     getCost() {
  3.         return this.value + this.value * this.runCosts / 100
  4.     },
  5.  
  6.  
  7.     "value":150,
  8.     "runCosts":32,
  9.  
  10.  
  11.     verySlowFunction() {
  12.         for(let i=0; i<1e10; i++) {
  13.  
  14.         }
  15.         return this.getCost();
  16.     }
  17. }
  18.  
  19. function cachDecorator(func) {
  20.     let cache = new Map();
  21.  
  22.     return function(x) {
  23.         if(cache.has(x)) {
  24.             return cache.get(x);
  25.         }
  26.         let result = func.call(this, x);
  27.         cache.set(x, result);
  28.         return result;
  29.     };
  30.  
  31.    
  32. }
  33.  
  34.  
  35. car.verySlowFunction = cachDecorator(car.verySlowFunction);
  36.  
  37. test = function() {alert(car.verySlowFunction())}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement