Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fib Now
(version: 0)
Comparing performance of:
Fib Base vs Fib memoized 1 vs Fib memoized Tommy
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Fib Base
const fibonacci = (n) => { if (n <= 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); }; fibonacci(20);
Fib memoized 1
const memoize = function(func) { var stringifyJson = JSON.stringify, cache = {}; var cachedfun = function() { var hash = stringifyJson(arguments); return hash in cache ? cache[hash] : (cache[hash] = func.apply(this, arguments)); }; cachedfun.__cache = function() { cache.remove || (cache.remove = function() { var hash = stringifyJson(arguments); return delete cache[hash]; }); return cache; }.call(this); return cachedfun; }; const fibonacci = memoize((n) => { if (n <= 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); }); fibonacci(20);
Fib memoized Tommy
const memoize = function(func) { const cache= {} return (...args) => { const n = args[0] if (n in cache) { return cache[n]; } else { const result = func(n) cache[n] = result return result } } }; const fibonacci = memoize((n) => { if (n <= 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); }); fibonacci(20);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Fib Base
Fib memoized 1
Fib memoized Tommy
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll dive into explaining the benchmark. **Benchmark Overview** The benchmark tests the performance of two implementations of the Fibonacci sequence: a baseline implementation (`Fib Base`) and two memoized versions (`Fib memoized 1` and `Fib memoized Tommy`). The benchmark uses a JavaScript function to calculate the nth Fibonacci number, which is then executed multiple times in a loop. **Options Compared** The three options being compared are: 1. **Baseline Implementation (`Fib Base`)**: This implementation calculates each Fibonacci number from scratch, without any optimization or caching. 2. **Memoized Version 1 (`Fib memoized 1`)**: This implementation uses a technique called memoization to cache previously computed Fibonacci numbers. It stores the results in an object and reuses them instead of recalculating them. 3. **Memoized Version 2 (`Fib memoized Tommy`)**: This implementation is similar to Memoized Version 1, but with some minor differences in the caching logic. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Baseline Implementation (`Fib Base`)**: + Pros: Simple to implement, easy to understand. + Cons: Inefficient, as it recalculates the same Fibonacci numbers multiple times. * **Memoized Version 1 (`Fib memoized 1`)**: + Pros: Improves performance by caching previously computed Fibonacci numbers. + Cons: Requires additional memory to store the cache, and the caching logic is not optimized. * **Memoized Version 2 (`Fib memoized Tommy`)**: + Pros: Similar performance improvement as Memoized Version 1, but with some minor optimizations. + Cons: Still requires additional memory for caching, and the caching logic is similar to Memoized Version 1. **Library/Technique Used** Neither of the memoized versions uses a separate library. Instead, they rely on a simple object to store the cached values. The `memoize` function in both implementations creates this object and manages the caching logic. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's purely functional programming with loops. **Other Alternatives** If you were to implement a memoized Fibonacci sequence, some alternative approaches could include: * Using a library like `lru-cache` to handle caching and storage. * Implementing a more advanced caching strategy, such as using a Least Recently Used (LRU) cache. * Using a specialized data structure, such as a trie or a hash table, to store the cached values. These alternatives would provide additional optimization opportunities, but also introduce new complexity and overhead.
Related benchmarks:
DateTime vs Date
Moment vs Luxon add day / add sec
Time libraries simple format
Luxon vs Moment Common Operations Benchmark Individual
date-fns subMilli new Date() vs date.now
Comments
Confirm delete:
Do you really want to delete benchmark?