Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
caching string interpolation vs not
(version: 0)
Comparing performance of:
no caching vs caching
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var objectString = { "reportActions_1234": { action: "view", timestamp: "2024-07-17T10:15:30Z", user: "JohnDoe" }, "reportActions_5678": { action: "edit", timestamp: "2024-07-17T11:20:45Z", user: "JaneSmith" }, "reportActions_91011": { action: "delete", timestamp: "2024-07-17T12:25:50Z", user: "AlexJohnson" }, "reportActions_1213": { action: "create", timestamp: "2024-07-17T13:30:55Z", user: "EmilyBrown" }, "reportActions_1415": { action: "share", timestamp: "2024-07-17T14:35:00Z", user: "ChrisDavis" }, "reportActions_1235": { action: "view", timestamp: "2024-07-17T10:15:30Z", user: "JohnDoe" }, "reportActions_5679": { action: "edit", timestamp: "2024-07-17T11:20:45Z", user: "JaneSmith" }, "reportActions_91021": { action: "delete", timestamp: "2024-07-17T12:25:50Z", user: "AlexJohnson" }, "reportActions_1214": { action: "create", timestamp: "2024-07-17T13:30:55Z", user: "EmilyBrown" }, "reportActions_1416": { action: "share", timestamp: "2024-07-17T14:35:00Z", user: "ChrisDavis" }, "report_1234": { action: "view", timestamp: "2024-07-17T10:15:30Z", user: "JohnDoe" }, "report_5678": { action: "edit", timestamp: "2024-07-17T11:20:45Z", user: "JaneSmith" }, "report_91011": { action: "delete", timestamp: "2024-07-17T12:25:50Z", user: "AlexJohnson" }, "report_1213": { action: "create", timestamp: "2024-07-17T13:30:55Z", user: "EmilyBrown" }, "report_1415": { action: "share", timestamp: "2024-07-17T14:35:00Z", user: "ChrisDavis" }, "report_1235": { action: "view", timestamp: "2024-07-17T10:15:30Z", user: "JohnDoe" }, "report_5679": { action: "edit", timestamp: "2024-07-17T11:20:45Z", user: "JaneSmith" }, "report_91021": { action: "delete", timestamp: "2024-07-17T12:25:50Z", user: "AlexJohnson" }, "report_1214": { action: "create", timestamp: "2024-07-17T13:30:55Z", user: "EmilyBrown" }, "report_1416": { action: "share", timestamp: "2024-07-17T14:35:00Z", user: "ChrisDavis" } }; var reportActionIds = [ "1234", "5678", "91011", "1213", "1415", "1235", "5679", "91021", "1214", "1416" ]; var reportIds = [ "1234", "5678", "91011", "1213", "1415", "1235", "5679", "91021", "1214", "1416" ];
Tests:
no caching
function getString(collection, id) { return `${collection}${id}`; } for (let i of reportActionIds) { objectString[getString("reportActions_", i)]; } for (let i of reportIds) { objectString[getString("report_", i)]; } for (let i of reportActionIds) { objectString[getString("reportActions_", i)]; } for (let i of reportIds) { objectString[getString("report_", i)]; }
caching
const cache = {}; function getString(collection, id) { if (!cache[collection]) { cache[collection] = {}; } if (cache[collection][id]) { return cache[collection][id]; } cache[collection][id] = `${collection}${id}`; return cache[collection][id]; } for (let i of reportActionIds) { objectString[getString("reportActions_", i)]; } for (let i of reportIds) { objectString[getString("report_", i)]; } for (let i of reportActionIds) { objectString[getString("reportActions_", i)]; } for (let i of reportIds) { objectString[getString("report_", i)]; } console.log(cache);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
no caching
caching
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
no caching
284130.7 Ops/sec
caching
25459.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what we're dealing with here. We have two test cases: 1. **No caching**: This is the original implementation where each call to `getString` creates a new string without storing any results in a cache. The function simply returns the concatenated string. 2. **Caching**: This implementation uses a simple object-based cache to store results of previous calls to `getString`. If the result for a given key is already cached, it returns the cached value instead of recalculating it. Now, let's analyze the benchmark: The two test cases are identical except for how they implement the `getString` function. One uses no caching (`no caching`), while the other uses caching (`caching`). Looking at the latest benchmark results, we can see that: * The **No Caching** test executes approximately 284,130 times per second. * The **Caching** test executes approximately 25,459 times per second. This significant difference in execution rates suggests that the caching implementation reduces the overhead of repeated calculations by storing and reusing previously computed results. In other words, caching speeds up the function by avoiding redundant computations. However, it's also possible to ask: What are the actual numbers? How many more times does **No Caching** execute compared to **Caching**? Let's calculate that: 284,130 (No Caching) - 25,459 (Caching) = 258,671 So, on average, **No Caching** executes around 8.3 times more than the **Caching** implementation. But what about memory usage? Does caching really come at a cost? We can see that caching creates an additional object (`cache`) that is referenced throughout the code. The size of this object can be significant depending on how many unique keys are used in the `getString` function. In general, while caching can provide substantial speedups for certain use cases, it may require more memory due to the storage of cached values.
Related benchmarks:
delete vs spread without vs spread override
delete vs spread without vs spread override 2
string interpolation vs direct access
caching vs no cachink ye
Comments
Confirm delete:
Do you really want to delete benchmark?