HTML Preparation code:
AخA
 
1
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
Tests:
  • foreach

    x
     
    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');
  • flatten

     
    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');
  • some

     
    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');
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    foreach
    flatten
    some

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0
Firefox 95 on Mac OS X 10.15
View result in a separate tab
Test name Executions per second
foreach 937347.9 Ops/sec
flatten 1678873.1 Ops/sec
some 1277660.9 Ops/sec