shawnbot/d3-cartogram — pull request #11 by calvinmetcalf (2013-05-16 16:44:02+00:00)
Title: optimizations from my state one

thanks to help from @natevw I was able to optimize the version I did of MA from 8 seconds to <2, these are the optimizations, which mainly involved removing `map` and `forEach` functions and replacing them with while loops, the two biggest speedups were

1. Instead of calculating vector as a map of meta which we reduced, reduce directly on vector.
2. Instead of calculating theta, from `arctan2(dx,dy)` and then latter doing `sin(theta)` and `cos(theta)`, sin(arctan()) and cos(arctan()) can be simplified as

```javascript
function sinArctan(dx,dy){
var div = dx/dy;
return (dy>0)?
(div/Math.sqrt(1+(div*div))):
(-div/Math.sqrt(1+(div*div)));
}
function cosArctan(dx,dy){
var div = dx/dy;
return (dy>0)?
(1/Math.sqrt(1+(div*div))):
(-1/Math.sqrt(1+(div*div)));
}
```