memoizeLast.js (600B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 export function memoizeLast(fn) { 6 let lastArgs; 7 let lastResult; 8 9 const memoized = (...args) => { 10 if ( 11 lastArgs && 12 args.length === lastArgs.length && 13 args.every((arg, i) => arg === lastArgs[i]) 14 ) { 15 return lastResult; 16 } 17 18 lastArgs = args; 19 lastResult = fn(...args); 20 21 return lastResult; 22 }; 23 24 return memoized; 25 } 26 27 export default memoizeLast;