Skip to content

Poor Performance of Classes vs Plain JavaScript Objects in Immer 10.0.2 vs 6.0.3 #1071

Closed
@bradedelman

Description

🐛 Bug Report

This is sample that illustrates a problem we are having with our real use of Immer, but is simplified for this issue report.

We have a piece of data with 12 simple numeric fields.
We have a version of it that is a plain JavaScript object.
We have a version of it that is a JavaScript class.

In Immer 6.0.2, mutating with produce is about the same speed with both the Class Instance and Plain JS Object.
In Immer 10.0.2, mutating with produce, the class instance is about 5x slower than the Plain JS Object.

Note that in Immer 9.0.21, plain objects are slower than 6.0.2, but about the same speed as Class Instances, so it seems this is an active area of development. It's nice that 10.0.2 is faster than 6.0.2 for Plain JS Object. However, the perf issue that remains for class instances is significant.

We are targeting 120,000 object mutations per second (on top of other work) and that 5x multiple has a meaningful, negative performance impact. (We have 2000 objects and there's a target 60 FPS update).

The class is very simple here. Nothing fancy. No getters, etc.

class Foo {
	[immer.immerable] = true;	
  	a = 1;
  	b = 2;
  	c = 3;
  	d = 4;
  	e = 5;
  	f = 6;
  	g = 7;
  	h = 8;
  	i = 9;
  	j = 10;
  	k = 11;
  	l = 12;
}

vs an instance of this plain JS Object:

{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5,
  "f": 6,
  "g": 7,
  "h": 8,
  "i": 9,
  "j": 10,
  "k": 11,
  "l": 12
}

Link to repro

(be sure to view the JS Fiddle Console to see the console output)

Here's our sample code running JS Fiddle with Immer 6.0.3:
https://jsfiddle.net/csz37eb0/1/

"class", 232.2000002861023
"object", 221
"x slower with class", 1.0506787343262547

Here's our sample code running JS Fiddle with Immer 10.0.2:
https://jsfiddle.net/bop856g1/2/

"class", 489.5
"object", 93.7000002861023
"x slower with class", 5.2241195144649675

To Reproduce

Sample code provided in the JS Fiddles.

Observed behavior

mutating a simple class instance is 5x slower than equivalent plain JS Object.
5x is significant. Also, Immer 6.0.3 had them about the same speed.

Expected behavior

Same or almost same speed for a simple class like this.

Environment

  • Immer version 10.0.2
  • Mac M1
  • Chrome Version 116.0.5845.187.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions