negative modulo (v97)

Revision 97 of this benchmark created on


Preparation HTML

<script scr="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

<script>



var regex = /\${([^}]*)}/g;

function parseTemplateObject(templateObject, context){
  var startTime = Date.now();

  var {
    window,
    document,
    setTimeout,
    setInterval,
    currentRecord,
    currentUser,
    iteratorObject,
    router,
    parentRecord,
    resources,
  } = context;
 
 console.log(templateObject,'T');
 try{
  if (_.isEmpty(templateObject)) {
    return templateObject;
  }

  function recursivelyIterate(object, acc = {}) {
    if (!_.isObject(object)) {
      return object.replace(regex, (match, group) => eval(`${group}`));
    }

    _.forEach(object, (value, key) => {
      if (_.isObject(value)) {
        if (Array.isArray(value)) {
          acc[key] = value.map(element => recursivelyIterate(element));
        } else {
          acc[key] = {}; // currentRecord:{}
          //f the property is an object, recursively iterate over its properties
          acc[key] = recursivelyIterate(value);
        }
      } else {
        // eslint-disable-next-line no-lonely-if -- legacy code
        if (typeof value === 'string') {
          //only exp
          if (/^\$\{[^}]*\}$/.test(value)) {
            acc[key] = eval(value.substring(2, value.length - 1));
          } else {
            //exp having some string appended or prepended
            acc[key] = value.replace(regex, (match, group) => eval(`${group}`));
          }
        } else {
          acc[key] = value;
        }
      }
    });

    return acc;
  };

  return recursivelyIterate(templateObject);
  }
  catch(e){
  	console.error(e);
  }
};

function templateObject2(
  templateObject,
  context
){
	console.log('test',templateObject,context,_);
  if (_.isEmpty(templateObject)) {
    return templateObject;
  }

  try {
    return JSON.parse(_.template(JSON.stringify(templateObject))(context)) ??{};
  } catch (e) {
    return templateObject;
  }
};


</script>

Test runner

Ready to run.

Testing in
TestOps/sec
eval approach
const TEMPLATE_OBJECT = {
  cell: {
    variant: "image",
    imageURL:
      "${iteratorObject?.linkedEntityId?.entity?.resolvedPersonEntity?.avatarUrl}",
    label:
      "${iteratorObject?.linkedEntityId?.entity?.resolvedPersonEntity?.name ?? iteratorObject?.email}",
    component: "TableTextWithImage",
  },
  displayConfig: {
    isHinged: false,
  },
};

const CONTEXT = {
  iteratorObject: {
  	linkedEntityId:{
  		entity:{
  			resoledPersonEntity:{
  				avatarUrl:'123'
  			}
  		}
  	}
  }
};



parseTemplateObject(TEMPLATE_OBJECT, CONTEXT);
ready
_template
const TEMPLATE_OBJECT = {
  cell: {
    variant: "image",
    imageURL:
      "${iteratorObject?.linkedEntityId?.entity?.resolvedPersonEntity?.avatarUrl}",
    label:
      "${iteratorObject?.linkedEntityId?.entity?.resolvedPersonEntity?.name ?? iteratorObject?.email}",
    component: "TableTextWithImage",
  },
  displayConfig: {
    isHinged: false,
  },
};

const CONTEXT = {
  iteratorObject: {
  	linkedEntityId:{
  		entity:{
  			resoledPersonEntity:{
  				avatarUrl:'123'
  			}
  		}
  	}
  }
};


templateObject2(TEMPLATE_OBJECT, CONTEXT);
ready

Revisions

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