rename-properties-vs-creating-new-object

JavaScript performance comparison

Test case created by viatropos

Info

Is it better to just create a new object, or to rename properties? Example is, formatting property names from underscores to camel-case (from server-side to client-side).

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    window.sourceObject = null;
   
    window.createSourceObject = function() {
      return {
          a_property: 'string'
        , b_property: null
        , c_property: 0
        , d_property: 1
        , e_property: new Date
        , f_property: 'string'
        , g_property: 100.00
        , h_property: -20.2
        , i_property: -4
        , j_property: new Date
      };
    }
};

Benchmark.prototype.teardown = function() {
    window.sourceObject = null;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
create new object
sourceObject = createSourceObject();

sourceObject = {
    aProperty: sourceObject.a_property
  , bProperty: sourceObject.b_property
  , cProperty: sourceObject.c_property
  , dProperty: sourceObject.d_property
  , eProperty: sourceObject.e_property
  , fProperty: sourceObject.f_property
  , gProperty: sourceObject.g_property
  , hProperty: sourceObject.h_property
  , iProperty: sourceObject.i_property
  , jProperty: sourceObject.j_property
};
pending…
rename properties
sourceObject = createSourceObject();

sourceObject.aProperty = sourceObject.a_property;
sourceObject.bProperty = sourceObject.b_property;
sourceObject.cProperty = sourceObject.c_property;
sourceObject.dProperty = sourceObject.d_property;
sourceObject.eProperty = sourceObject.e_property;
sourceObject.fProperty = sourceObject.f_property;
sourceObject.gProperty = sourceObject.g_property;
sourceObject.hProperty = sourceObject.h_property;
sourceObject.iProperty = sourceObject.i_property;
sourceObject.jProperty = sourceObject.j_property;

delete sourceObject.a_property;
delete sourceObject.b_property;
delete sourceObject.c_property;
delete sourceObject.d_property;
delete sourceObject.e_property;
delete sourceObject.f_property;
delete sourceObject.g_property;
delete sourceObject.h_property;
delete sourceObject.i_property;
delete sourceObject.j_property;
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. Here’s a list of current revisions for this page:

0 comments

Add a comment