var symbol = new esri.symbol.SimpleMarkerSymbol(); symbol.setSize(10); symbol.setColor(new dojo.Color([255,0,0]));
var symbol = new esri.symbol.SimpleMarkerSymbol().setSize(10).setColor(new dojo.Color([255,0,0]));
When chaining method calls, you need to determine a balance between efficiency and readability of your code. Your code might be more readable and maintainable if you avoid chaining; however, you will forfeit the performance benefit that chaining offers.
I understand in Java, writing a chain method vs the stack of methods should compile down to the same bytecode. However, since this is a scripting language, does this really hold water? Also, if it does, is it worth sacrificing the readability for the code for the performance of that section of code?
Edit: After some performance testing, I have found that it doesn't really matter whether or not the methods are chained or not. (One time one would be faster, another time the other was faster)
Chaining methods like this CAN improve performance, but only in limited scenarios where the API you're using is built to provide this functionality. The first example that comes to mind is with jQuery.
Calling $("#test") takes time to return the jquery object that references #test.
When you chain a method, it reuses that object.
Check out this test I made as an example.
©2020 All rights reserved.