Rich Text Attribute Clone

Benchmark created by Erik on


Description

Speed testing for a function in https://github.com/ottypes/rich-text

Setup

var is = {
    object: function (value) {
        if (!value) return false;
        return (typeof value === 'function' || typeof value === 'object');
      },
    }
    
    
    var defaultClone = function (attributes, keepNull) {
          if (!is.object(attributes)) return {};
          return Object.keys(attributes).reduce(function (memo, key) {
            if (attributes[key] !== undefined && (attributes[key] !== null || keepNull)) {
              memo[key] = attributes[key];
            }
            return memo;
          }, {});
    }
    
    var loopClone = function (attributes, keepNull) {
      var target = {};
      if (!is.object(attributes)) return target;
      for (var key in attributes) {
        if (attributes[key] !== undefined && (attributes[key] !== null || keepNull)) {
          target[key] = attributes[key];
        }
      }
      return target;
    }
    
    var testObject = { bold: true, underline: null, random:'foobar',color: Math.floor(Math.random()*40)};

Test runner

Ready to run.

Testing in
TestOps/sec
Default Functional
var clone = defaultClone(testObject);
ready
Loop
var clone = loopClone(testObject);
ready
Default Functional Null
var clone = defaultClone(testObject,true);
ready
Loop Clone Null
var clone = loopClone(testObject,true);
ready

Revisions

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