I was wondering why the d3.identity function d3.identity not part of the library, and could not find a reason not to have it.
In terms of performance, defining an identity function provides better performance than reusing the Object constructor. This is not a big deal if you reuse the same authentication function for different types. The following are performance tests .
So, in my case, I abuse D3 and add the function myself:
d3.identity = function(d) { return d; }
If you use underscore , you can also use _. identity .
Regarding the use of the Object constructor, I believe that this creates a new, unnecessary object every time it calls up waste memory and processor time, both for creating and for garbage collection. This can be optimized for immutable types, such as numbers at some time intervals.
EDIT Phrogz has a short article that shows a useful shortcut for reducing the amount of lambda when working with D3, which includes an identification function.
source share