michael@0: String.prototype.repeat = function(num) { michael@0: return new Array(num + 1).join(this); michael@0: } michael@0: michael@0: function set_to_length(length, frag_size) michael@0: { michael@0: var fragment = "'" + "x".repeat(frag_size) + "' + "; michael@0: var frags = Math.floor((length - 1)/frag_size); michael@0: var code = "var x = " + fragment.repeat(frags) + "'" + michael@0: "x".repeat(length - frags * frag_size) + "';"; michael@0: michael@0: try { michael@0: eval(code); michael@0: } michael@0: catch(err) { michael@0: if (err.message && err.message == "Out of memory") michael@0: return -1; michael@0: if (err == "out of memory") michael@0: return -1; michael@0: throw(err); /* Oops, broke something. */ michael@0: } michael@0: michael@0: return code.length; michael@0: } michael@0: michael@0: var first_fail; michael@0: var first_pass; michael@0: var frag_size; michael@0: var pass_code_length; michael@0: michael@0: function search_up() michael@0: { michael@0: if (set_to_length(first_fail, frag_size) < 0) { michael@0: setTimeout(binary_search, 0); michael@0: return; michael@0: } michael@0: michael@0: first_fail *= 2; michael@0: } michael@0: michael@0: function binary_search() michael@0: { michael@0: if (first_fail - first_pass > 1) { michael@0: var length = (first_pass + first_fail) / 2; michael@0: var code_len = set_to_length(length, frag_size); michael@0: if (code_len > 0) { michael@0: first_pass = length; michael@0: pass_code_length = code_len; michael@0: } else michael@0: first_fail = length; michael@0: setTimeout(binary_search, 0); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: function run_find_limit() michael@0: { michael@0: frag_size = 64; michael@0: first_fail = 65536; /* A guess */ michael@0: first_pass = 0; michael@0: pass_code_length = 0; michael@0: michael@0: for (var i=0; i<5; i++) michael@0: search_up(0); michael@0: } michael@0: michael@0: run_find_limit();