collection-testing

JavaScript performance comparison

Test case created by rufman

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
    const data =  [
      {
        timestamp: 10,
        updates: {
          xARnYWxh: { key: 'gala', value: { color: 'red' } },
          'xANtYWM=': { key: 'mac', value: { color: 'green' } },
          xARmdWpp: { key: 'fuji', value: { color: 'red' } },
        },
      },
      {
        timestamp: 20,
        updates: {
          'xAZnb2xkZW4=': { key: 'golden', value: { color: 'green' } },
          'xAZyZWREZWw=': { key: 'redDel', value: { color: 'red' } },
        },
        deletes: {
          'xANtYWM=': { key: 'mac' },
        },
      },
      {
        timestamp: 25,
        deletes: {}, // wildcard delete.  Delete entire collection.
      },
      {
        timestamp: 27,
        updates: {
          'xAZnb2xkZW4=': { key: 'golden', value: { color: 'green' } },
        },
        deletes: {}, // wildcard delete.  Delete entire collection, then add updates.
      },
      {
        timestamp: 30,
        updates: {
          'xAZncmFubnk=': { key: 'granny', value: { color: 'green' } },
          xARnYWxh: { key: 'gala', value: { color: 'red' } },
        },
      },
      {
        timestamp: 40,
        updates: {
          'xAVtdXRzdQ==': { key: 'mutsu', value: { color: 'green' } },
        },
      },
      {
        timestamp: 50,
        deletes: {
          'xAVtdXRzdQ==': { key: 'mutsu' },
        },
      },
      {
        timestamp: 60,
        updates: {
          'xAhtY2ludG9zaA==': { key: 'mcintosh', value: { color: 'red' } },
        },
        deletes: {
          xARnYWxh: { key: 'gala' },
          'xAhtY2ludG9zaA==': { key: 'mcintosh' }, // delete key not present
        },
      },
    ];
    
    const start = 20;
  const end = 60;
  const maxChanges = undefined;
  
  function isLiveTime(time) {
    return time === undefined || time === null || time === 'LIVE_TIME';
  }
  
       

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
old
if (_.isEmpty(data)) {
    return [];
  }

  let startIndex = 0;
  let endIndex = data.length;

  if (start) {
    startIndex = _.sortedIndexBy(data, { timestamp: start }, 'timestamp');
  }

  if (!isLiveTime(end)) {
    endIndex = _.sortedLastIndexBy(data, { timestamp: end }, 'timestamp');
  }

  const changes = [];
  const changesSlice = _.slice(data, startIndex, endIndex);
  const changesSliceLen = changesSlice.length;

  for (let i = 0; i < changesSliceLen; i++) {
    const datum = changesSlice[i];
    // Log deletes
    if (datum.deletes) {
      const deleteKeys = Object.keys(datum.deletes);
      const deleteKeysLen = deleteKeys.length;

      for (let d = 0; d < deleteKeysLen; d++) {
        const key = deleteKeys[d];
        const deletion = datum.deletes[key];

        changes.push({
          timestamp: datum.timestamp,
          serializedKey: key,
          key: _.get(deletion, 'key'),
          type: 'COLLECTION_DELETION',
        });
      }
    }

    // Log additions and modifications
    if (datum.updates) {
      const updateKeys = Object.keys(datum.updates);
      const updateKeysLen = updateKeys.length;

      for (let u = 0; u < updateKeysLen; u++) {
        const key = updateKeys[u];
        const update = datum.updates[key];

        changes.push({
          type: 'COLLECTION_UPDATE',
          timestamp: datum.timestamp,
          key: update.key,
          serializedKey: key,
          value: update.value,
        });
      }
    }
  }

  if (maxChanges && changes.length > maxChanges) {
    if (start !== undefined) {
      return _.slice(changes, 0, maxChanges);
    }

    if (end) {
      return _.slice(changes, changes.length - maxChanges);
    }

    return _.slice(changes, 0, maxChanges);
  }
pending…
new
if (!data.length) {
    return [];
  }

  let startIndex = 0;
  let endIndex = data.length;

  if (start) {
    startIndex = _.sortedIndexBy(data, { timestamp: start }, 'timestamp');
  }

  if (!isLiveTime(end)) {
    endIndex = _.sortedLastIndexBy(data, { timestamp: end }, 'timestamp');
  }

  const changes = [];
  const changesSlice = data.slice(startIndex, endIndex);
  const changesSliceLen = changesSlice.length;

  for (let i = 0; i < changesSliceLen; i++) {
    const datum = changesSlice[i];
    // Log deletes
    if (datum.deletes) {
      const deleteKeys = Object.keys(datum.deletes);
      for (let d = 0; d < deleteKeys.length; d++) {
        const key = deleteKeys[d];
        const deletion = datum.deletes[key];

        changes.push({
          timestamp: datum.timestamp,
          serializedKey: key,
          key: _.get(deletion, 'key'),
          type: 'COLLECTION_DELETION',
        });
      }
    }

    // Log additions and modifications
    if (datum.updates) {
      const updateKeys = Object.keys(datum.updates);
      for (let u = 0; u < updateKeys.length; u++) {
        const key = updateKeys[u];
        const update = datum.updates[key];

        changes.push({
          type: 'COLLECTION_UPDATE',
          timestamp: datum.timestamp,
          key: update.key,
          serializedKey: key,
          value: update.value,
        });
      }
    }
  }

  if (maxChanges && changes.length > maxChanges) {
    if (start !== undefined) {
      return changes.slice(0, maxChanges);
    }

    if (end) {
      return changes.slice(changes.length - maxChanges);
    }

    return changes.slice(0, maxChanges);
  }
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments