Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let car = {
- getCost() {
- return this.value + this.value * this.runCosts / 100
- },
- "value":150,
- "runCosts":32,
- verySlowFunction() {
- for(let i=0; i<1e10; i++) {
- }
- return this.getCost();
- }
- }
- function cachDecorator(func) {
- let cache = new Map();
- return function(x) {
- if(cache.has(x)) {
- return cache.get(x);
- }
- let result = func.call(this, x);
- cache.set(x, result);
- return result;
- };
- }
- car.verySlowFunction = cachDecorator(car.verySlowFunction);
- test = function() {alert(car.verySlowFunction())}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement