diff --git a/lib/codemirror.js b/lib/codemirror.js
index 82405ff43453c5379974dc9afaa2f416d4c8470d..7a0919aababd4f05bfe73ef549f8df12f41c0ee1 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -72,7 +72,7 @@ var CodeMirror = (function() {
     var editing, bracketHighlighted;
     // Tracks the maximum line length so that the horizontal scrollbar
     // can be kept static when scrolling.
-    var maxLine = "";
+    var maxLine = "", maxWidth;
 
     // Initialize the content.
     operation(function(){setValue(options.value || ""); updateInput = false;})();
@@ -463,12 +463,12 @@ var CodeMirror = (function() {
       for (var i = from.line, e = i + newText.length; i < e; ++i) {
         var l = lines[i].text;
         if (l.length > maxLineLength) {
-          maxLine = l; maxLineLength = l.length;
+          maxLine = l; maxLineLength = l.length; maxWidth = null;
           recomputeMaxLength = false;
         }
       }
       if (recomputeMaxLength) {
-        maxLineLength = 0; maxLine = "";
+        maxLineLength = 0; maxLine = ""; maxWidth = null;
         for (var i = 0, e = lines.length; i < e; ++i) {
           var l = lines[i].text;
           if (l.length > maxLineLength) {
@@ -769,9 +769,9 @@ var CodeMirror = (function() {
         updateGutter();
       }
 
-      var textWidth = stringWidth(maxLine);
-      if (textWidth > scroller.clientWidth) {
-        lineSpace.style.width = textWidth + "px";
+      if (maxWidth == null) maxWidth = stringWidth(maxLine);
+      if (maxWidth > scroller.clientWidth) {
+        lineSpace.style.width = maxWidth + "px";
         // Needed to prevent odd wrapping/hiding of widgets placed in here.
         code.style.width = scroller.scrollWidth + "px";
       } else {