SOFTKEKS TESTS #1 - urlList to tree

JavaScript performance comparison

Test case created by dreszczyk

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  const urlList = [ "/api/reset_password/init", "/api/reset/password/external/system", "/api/user/channel_history", "/api/rdf/session_create", "/api/rdf_application/person_data", "/api/channel_auth_tool_list", "/api/rdf/application/summary", "/api/user/external_id_policy", "/api/reset/password/channel", "/api/user/account/get", "/api/login/finalize_auth", "/api/user/channel/history", "/api/rdf/application/firm_finances", "/api/channel/application/list", "/api/rdf_offer_search", "/api/user/channel_history", "/api/rdf/session_create", "/api/rdf_application/person_data", "/api/channel_auth_tool_list", "/api/config", "/api/rdf_application/summary", "/api/user/account/history", "/api/reset_password/channel", "/api/rdf_offer_rate_calculate", "/api/config", "/api/user/account_get", "/api/rdf_session_create", "/api/channel_password_policy", "/api/user/context_history", "/api/rdf_session_get", "/api/user_external_id_policy", "/api/ping", "/api/fake_login", "/api/user_context_history", "/api/user_channel_history", "/api/rdf_offer_details", "/api/user/channel/list", "/api/user_context_role_list", "/api/rdf_application/person_finances", "/api/user/context_list", "/api/reset_password/external_system", "/api/user/context/role/list", "/api/user_account_list", "/api/rdf/offer_details", "/api/set_password/new_password", "/api/channel/list", "/api/user/context/history", "/api/login/auth_tool", "/api/set_password/init", "/api/rdf/application/person_finances", "/api/rdf_application/representation_data", "/api/rdf_application/firm_finances", "/api/user/event_list", "/api/rdf/session_get", "/api/user_channel_list", "/api/channel/password/policy", "/api/rdf/application/start", "/api/rdf_application/firm_data", "/api/user/account_list", "/api/rdf/application/representation_data", "/api/user/account/stats", "/api/rdf/application/documents", "/api/user_external_id_policy", "/api/ping", "/api/fake_login", "/api/user_context_history", "/api/user_channel_history", "/api/user_account_get", "/api/login/external_system", "/api/login/channel", "/api/user/external/id_policy", "/api/ping", "/api/user/context/list", "/api/user_context_list", "/api/login/init", "/api/password_history", "/api/rdf/application/firm_data", "/api/rdf_application/documents", "/api/channel/auth/tool/list", "/api/channel_list", "/api/set/password/init", "/api/reset/password/init", "/api/rdf/offer_rate_calculate", "/api/rdf_application/start", "/api/channel_password_set", "/api/user_account_history", "/api/rdf/application/person_data", "/api/user/channel_list", "/api/channel/password/set", "/api/user/context_role_list", "/api/channel_application_list", "/api/password_change", "/api/set/password/new/password", "/api/user/account/list", "/api/user/account_history", "/api/user/account_stats", "/api/logout", "/api/user_channel_history", "/api/user_account_get", "/api/login/external_system", "/api/login/channel", "/api/password_set", "/api/session_refresh", "/api/rdf/offer_search", "/api/user/event/list", "/api/user_account_stats", "/api/user_event_list"];
  
  const expectedResult = {"api":{"reset_password":{"init":{},"channel":{},"external_system":{}},"reset":{"password":{"external":{"system":{}},"channel":{},"init":{}}},"user":{"channel_history":{},"external_id_policy":{},"account":{"get":{},"history":{},"stats":{},"list":{}},"channel":{"history":{},"list":{}},"account_get":{},"context_history":{},"context_list":{},"context":{"role":{"list":{}},"history":{},"list":{}},"event_list":{},"account_list":{},"external":{"id_policy":{}},"channel_list":{},"context_role_list":{},"account_history":{},"account_stats":{},"event":{"list":{}}},"rdf":{"session_create":{},"application":{"summary":{},"firm_finances":{},"person_finances":{},"start":{},"representation_data":{},"documents":{},"firm_data":{},"person_data":{}},"offer_details":{},"session_get":{},"offer_rate_calculate":{},"offer_search":{}},"rdf_application":{"person_data":{},"summary":{},"person_finances":{},"representation_data":{},"firm_finances":{},"firm_data":{},"documents":{},"start":{}},"channel_auth_tool_list":{},"login":{"finalize_auth":{},"auth_tool":{},"external_system":{},"channel":{},"init":{}},"channel":{"application":{"list":{}},"list":{},"password":{"policy":{},"set":{}},"auth":{"tool":{"list":{}}}},"rdf_offer_search":{},"config":{},"rdf_offer_rate_calculate":{},"rdf_session_create":{},"channel_password_policy":{},"rdf_session_get":{},"user_external_id_policy":{},"ping":{},"fake_login":{},"user_context_history":{},"user_channel_history":{},"rdf_offer_details":{},"user_context_role_list":{},"user_account_list":{},"set_password":{"new_password":{},"init":{}},"user_channel_list":{},"user_account_get":{},"user_context_list":{},"password_history":{},"channel_list":{},"set":{"password":{"init":{},"new":{"password":{}}}},"channel_password_set":{},"user_account_history":{},"channel_application_list":{},"password_change":{},"logout":{},"password_set":{},"session_refresh":{},"user_account_stats":{},"user_event_list":{}}};
  

};

Benchmark.prototype.teardown = function() {
  clearTimeout(ui.endTimerId);
  ui.endTimerId = setTimeout( ()=>{
      let isDone = true;
      for(let i=0; i<ui.benchmarks.length; i++) {
          if (ui.benchmarks[i].running) {
              isDone = false;
              break;
          }
      }
  
      if (!isDone) {
          return;
      }
      console.log('----------------------------------------------');
      for(let i=0; i<ui.benchmarks.length; i++) {
          const testFn = new Function(Benchmark.prototype.setup + '\n' + ui.benchmarks[i].fn);
          const testName = ui.benchmarks[i].name;
          let isValidResult, testResult;
          try {
              testResult = testFn();
              isValidResult = JSON.stringify(testResult) === JSON.stringify(expectedResult);
          } catch (error) {
              console.log(`♿ Test "${testName}" error: `, error);
              continue;
          }
  
          if (isValidResult) {
              console.log(`✅ Test "${testName}" valid:`, testResult);
          } else {
              console.log(`❌ Test "${testName}" invalid: `, testResult);
          }
      }
  }, 100);

};
</script>

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
minimum working example
function assign(obj, keyPath, value) {
    lastKeyIndex = keyPath.length - 1;
    for (var i = 0; i < lastKeyIndex; ++i) {
        key = keyPath[i];
        if (!(key in obj)) {
            obj[key] = {}
        }
        obj = obj[key];
    }
    obj[keyPath[lastKeyIndex]] = value;
}

const convertToTree = (list) => {
    const tree = {};
    list.forEach(url => {
        assign(tree, url.replace('/api', 'api').split('/'), {});
    })
    return tree;
}

return convertToTree(urlList);
pending…
piotrek
const emptyObj = {};

const makeTree = R.reduce((acc, url) => {
    const path = R.tail(url.split('/'));
    const subTree = R.assocPath(path, emptyObj, acc);
    return R.mergeRight(acc, subTree)
}, {})

return makeTree(urlList);
pending…
marcin
let treeObject = {};
while (urlList.length) {
    let path = urlList[0].split('/');
    let current = treeObject;
    while (path.length > 1) {
        const first = path[0];
        path.shift();
        if (!current[first]) {
            current[first] = {};
        }
        current = current[first];
    }
    current[path[0]] = {};
    urlList.shift();
}
return treeObject[''];
pending…
wilku
const result = {};
for(let i = 0; urlList[i]; i++) {
    let lastObj = result;
    
    const props = urlList[i].split('/');
    for(let propIdx = 1; props[propIdx]; propIdx++) {
        const propName = props[propIdx];
        lastObj = lastObj[propName] || (lastObj[propName] = {});
    }
}
return result;
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