Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
vwd streamer
(version: 0)
Subscribe & Unsubscribe methods
Comparing performance of:
Origin code vs Optimized code
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fields = []; for (var i = 0; i < 1000; i++) { fields[i] = 'vwdId-' + i + '.vwdField'; } // create duplications fields = fields.concat(fields, fields);
Tests:
Origin code
var _streamerInstance = { _subscribe: function () {}, _unsubscribe: function () {}, _getCachedValue: function () {}, _update: function () {} }; var _subscribedFields = {}; var subscribe = function (fields) { if (!fields) { return } else if (typeof fields === "string") { fields = [fields] } var duplicateSubscriptions = []; for (var fieldIndex = 0; fieldIndex < fields.length; fieldIndex++) { var fieldName = fields[fieldIndex]; if (_subscribedFields[fieldName]) { duplicateSubscriptions.push(fieldIndex) } else { _subscribedFields[fieldName] = true } var cachedValue = _streamerInstance._getCachedValue(fieldName); if (cachedValue != null) { _streamerInstance._update(cachedValue.dataName, cachedValue.value, cachedValue.dataType, this) } } if (duplicateSubscriptions.length > 0) { fields = fields.slice(0); for (var i = duplicateSubscriptions.length - 1; i >= 0; i--) { fields.splice(duplicateSubscriptions[i], 1) } } _streamerInstance._subscribe(fields); } var unsubscribe = function (fields) { if (!fields) { return } else if (typeof fields === "string") { fields = [fields] } var invalidFields = []; for (var fieldIndex = 0; fieldIndex < fields.length; fieldIndex++) { var fieldName = fields[fieldIndex]; if (_subscribedFields[fieldName]) { delete _subscribedFields[fieldName] } else { invalidFields.push(fieldIndex) } } if (invalidFields.length > 0) { fields = fields.slice(0); for (var i = invalidFields.length - 1; i >= 0; i--) { fields.splice(invalidFields[i], 1) } } _streamerInstance._unsubscribe(fields); }; subscribe(fields); unsubscribe(fields);
Optimized code
var _streamerInstance = { _subscribe: function () {}, _unsubscribe: function () {}, _getCachedValue: function () {}, _update: function () {} }; var _subscribedFields = {}; var subscribe = function (fields) { if (!fields) { return } else if (typeof fields === "string") { fields = [fields] } var fieldsToSubscribe = []; var fieldsToSubscribeCount = 0; var length = fields.length; for (var fieldIndex = 0; fieldIndex < length; fieldIndex++) { var fieldName = fields[fieldIndex]; if (!_subscribedFields[fieldName]) { fieldsToSubscribe[fieldsToSubscribeCount] = fieldName; fieldsToSubscribeCount++; _subscribedFields[fieldName] = true } var cachedValue = _streamerInstance._getCachedValue(fieldName); if (cachedValue != null) { _streamerInstance._update(cachedValue.dataName, cachedValue.value, cachedValue.dataType, this) } } _streamerInstance._subscribe(fieldsToSubscribe); } var unsubscribe = function (fields) { if (!fields) { return } else if (typeof fields === "string") { fields = [fields] } var fieldsToUnsubscribe = []; var fieldsToUnsubscribeCount = 0; var length = fields.length; for (var fieldIndex = 0; fieldIndex < length; fieldIndex++) { var fieldName = fields[fieldIndex]; if (_subscribedFields[fieldName]) { fieldsToUnsubscribe[fieldsToUnsubscribeCount] = fieldName; fieldsToUnsubscribeCount++; delete _subscribedFields[fieldName] } } _streamerInstance._unsubscribe(fieldsToUnsubscribe); }; subscribe(fields); unsubscribe(fields);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Origin code
Optimized code
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):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition provides the setup for the test cases. It defines a JavaScript object `streamerInstance` with methods `_subscribe`, `_unsubscribe`, `_getCachedValue`, and `_update`. The test case also creates an object `_subscribedFields` to keep track of subscribed fields. **Test Cases** There are two test cases: 1. **Origin code** This test case uses the original implementation with the `duplicateSubscriptions` array to optimize field removals. 2. **Optimized code** This test case uses a modified implementation that avoids the need for the `duplicateSubscriptions` array, which reduces the overhead of removing fields. **Comparison** The two test cases are compared in terms of their execution performance, measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons** 1. **Origin code** * Pros: + Easier to understand and maintain due to its simplicity. * Cons: + Less efficient due to the use of `duplicateSubscriptions` array, which can lead to slower performance. 2. **Optimized code** * Pros: + More efficient than the original implementation due to reduced overhead when removing fields. * Cons: + More complex and harder to understand, making it more challenging to maintain. **Other Considerations** The benchmark result shows that the optimized code outperforms the origin code by a significant margin. This suggests that the modifications made to the `unsubscribe` function have a substantial impact on performance. **Library Usage** There is no explicit library usage in this benchmark definition, but it's worth noting that some JavaScript engines might have specific optimizations or features that could affect the execution performance of these test cases. **Device and Browser Variations** The benchmark result includes variations across different devices (Mac OS X 10.13.4) and browsers (Safari 11). This highlights the importance of testing on diverse platforms to ensure compatibility and performance consistency. By analyzing this benchmark, we can conclude that optimizations in the `unsubscribe` function can lead to significant improvements in execution performance, but these changes should be carefully considered to balance performance with maintainability and readability.
Related benchmarks:
vwd streamer
reduce + map filter
custom events vs pubSub custom
custom events vs pubSub custom with map and set
Comments
Confirm delete:
Do you really want to delete benchmark?