<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
const record = {
subscriptions: [{
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'aa',
],
}, ],
};
const isSubscribed = (subscriptionRecord, eventType) => {
const {
subscriptions
} = subscriptionRecord;
const events = [];
subscriptions.forEach(s => {
events.push(s.events);
});
return _.includes(events, eventType);
};
const result = isSubscribed(record, 'aa');
const record = {
subscriptions: [{
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'aa',
],
}, ],
};
const isSubscribed3 = (subscriptionRecord, eventType) => {
const events = _.flatten(subscriptionRecord.subscriptions.map(s => s.events))
return _.includes(events, eventType);
};
const result = isSubscribed3(record, 'aa');
const record = {
subscriptions: [{
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'wf',
],
}, {
events: [
'df',
'ff',
'gf',
'hf',
'rf',
'aa',
],
}, ],
};
const isSubscribed2 = (subscriptionRecord, eventType) => {
return _.some(subscriptionRecord.subscriptions, sub => _.includes(sub.events, eventType))
};
const t = isSubscribed2(record, 'aa');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
flatten | |
some |
Test name | Executions per second |
---|---|
foreach | 937347.9 Ops/sec |
flatten | 1678873.1 Ops/sec |
some | 1277660.9 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares three different approaches to check if an event is subscribed to a subscription record:
forEach
flatten
(using Lodash)some
Options Compared
forEach
: uses the forEach
method of arrays to iterate over the events in each subscription.flatten
(using Lodash): uses the flatten
function from Lodash to flatten the arrays of events in each subscription.forEach
, as it avoids creating arrays and iterating over them. Also, Lodash is a popular library with a wide range of utility functions.some
: uses the some
method of arrays to iterate over the events in each subscription.forEach
, but more concise and expressive.forEach
.Library Used
The benchmark uses Lodash, a popular JavaScript library that provides a wide range of utility functions for tasks such as array manipulation, string manipulation, and more. In this case, the flatten
function is used to flatten the arrays of events in each subscription.
Special JS Features or Syntax
None mentioned in the benchmark definition.
Other Considerations
Alternatives
If you're interested in exploring alternative approaches, here are some options:
reduce
: instead of using forEach
or some
, you could use the reduce
method to iterate over the events in each subscription.Keep in mind that these alternatives may have different performance characteristics and may require more code to implement.