Find numbers that are both Triangle numbers and Square numbers I've written a simple JavaScript function to find numbers that contain both the attributes of a Triangle (\$ n(n+1)/2 = x \$) and a Square (\$ n^2 = x \$) number.
The challenge was given in Matt Parker's Things to Make and Do in the Fourth Dimension.

function main() {
  for (var i = 1; i &lt; 1000000; i++) {
    var triangled = ( i * ( i + 1 ) ) / 2;
        if(Math.sqrt(triangled) % 1 === 0) {
         // print
      }
  }
}


Here's the JSFiddle of the same.

The algorithm n = 1000000 takes about ~35ms but gets considerably slower with higher values 

~300ms for n = 10000000

~3000ms for n = 100000000 

The only priority is performance. Any tips?
First, I tried to be mathematically clever (by trying something with the odd and evenness of triangular and square numbers). That resulted in me making a mistake and getting a bogus result.

Then I took a good look at your code. You are calling Math.sqrt for every iteration. That's really slow, so if we could work around that, it'd be a huge performance boost. And we can work around it, by keeping track of the next target to hit. 

Additionally, if we're gonna pass every integer anyway, why don't we add them as we go, rather than constantly recalculation triangle? A triangular number consists of all the whole positive integers at or below N, so by simply summing as we go, we can keep a calculated triangular number in memory without the need to recalculate every iteration.

The resulting code...

function main() {
var timer = Date.now();
  var triangleSum = 0;
  var productCounter = 1;
  var product = 1;
  for (var i = 1; i &lt; 100000000; i++) {
    triangleSum += i;
    if(triangleSum === product)
    {
        document.getElementById('log').innerHTML += '&lt;p&gt;' + Math.sqrt(triangleSum)+ ' and ' + i + ': ' + triangleSum + '&lt;/p&gt;';
    }
    if(triangleSum &gt;= product)
    {
        productCounter++;
        product = productCounter * productCounter;
    }
  }
  document.getElementById('log').innerHTML += '&lt;p&gt;&lt;b&gt;Time taken: ' + (Date.now() - timer) + ' milliseconds&lt;/b&gt;&lt;/p&gt;';
}


Runs in 380 ms compared to the 5346 ms it was giving me before.