0.0.1 • Published 8 years ago

lru-func v0.0.1

Weekly downloads
4
License
ISC
Repository
-
Last release
8 years ago

lru-func

lru-func

Like lru_cache in python

类似python中的lru_cache装饰器,但是由于js当前不支持装饰器,封装为一个函数

'use strict'

const lru_it = require('lru-func');

const arr_len = 30;

let call_num = 0,
    fibo_arr = [];


const fibo = lru_it((length) => {
  call_num += 1;
  return length <= 2 ? 1 : fibo(length - 1) + fibo(length - 2)
});

for (let i = 1; i <= arr_len; i++) {
  fibo_arr.push(fibo(i));
}

console.log(`with lru cache\nfibo(1:${arr_len})=${[fibo_arr]}\ninvoke fibo function ${call_num} times\n`)
with lru cache
fibo(1:30)=1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040
invoke fibo function 30 times