Are you saying im.getWidth() is doing some expensive calculation underneath, or that the cost of calling it is expensive in Javascript?
Since storing it will require width-1 less calls to im.getWidth() it is much more efficient no matter how you slice it.