Advertisement
Guest User

Untitled

a guest
May 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. var longRunningFunction = function (a, b, c) {
  2. longRunningFunction.cache = longRunningFunction.cache || {};
  3.  
  4. const cacheKey = JSON.stringify(arguments);
  5.  
  6. if (!longRunningFunction.cache[cacheKey]) {
  7. sleep(5000);
  8.  
  9. longRunningFunction.cache[cacheKey] = a * b * c;
  10. }
  11. return longRunningFunction.cache[cacheKey];
  12. }
  13.  
  14. function sleep(timeInMs) {
  15. const d1 = new Date();
  16. let d2 = new Date();
  17. while (d2.valueOf() < d1.valueOf() + timeInMs) {
  18. d2 = new Date();
  19. }
  20. }
  21.  
  22. console.log(longRunningFunction(1, 2, 3)); // czekamy 5 sekund
  23. console.log(longRunningFunction(1, 2, 3)); // wynik do razu
  24. console.log(longRunningFunction(10, 20, 3)); // czekamy 5 sekund
  25. console.log(longRunningFunction(3, 2, 3)); // czekamy 5 sekund
  26. console.log(longRunningFunction(30, 11, 3)); // czekamy 5 sekund
  27. console.log(longRunningFunction(3, 2, 3)); // wynik od razu
  28. console.log(longRunningFunction(3, 2, 3)); // wynik od razu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement