         context.fillRect(val2.x, val2.y, rectWidth, rectHeight)
 
 isort = ->
-        for j in [index...0]
-                swapRects(j - 1, j) if colours[j - 1].val > colours[j].val
-
-        index++
-        defer(isort) if index < colours.length
+        sort_context ?=
+                   i: 1
+                   j: 1
+                   count: 0
+
+        { i, j, count } = sort_context
+
+        swapRects(j - 1, j) if colours[j - 1].val > colours[j].val
+
+        if j == 1
+                return if i == colours.length - 1
+                sort_context.i++
+                sort_context.j = sort_context.i
+        else
+                sort_context.j--
+
+        sort_context.count++
+
+        if (sort_context.count % UPDATE_INTERVAL) == 0
+                defer(isort)
+        else
+                isort()
 
 # Selection sort
 # @author Bernhard Häussner (https://github.com/bxt)

        start = Date.now()

        initColours()
        defer(sort)

        checkDoneInterval = window.setInterval(checkDone, 10)

        window.clearTimeout(timeout) while (timeout = timeouts.pop())

        colours = []
        win = $(document)
        [width, height] = [$(document).width(), $(document).height()]

