Olov Lassus

Making v8bench restrict mode clean

01 Apr 2011

I’d like to know how well the restrict mode subset of JavaScript matches existing programs. I decided to test it on Google’s v8bench. I checked out the latest revision from SVN, and slapped "use restrict"; on top of every file. I ran that through restricter (part of JSShaper) before executing v8bench.

The benchmark then threw an exception whenever restrict mode detected that things were going on outside of its rules. I tweaked the v8bench source, and iterated.

I had to change only 8 lines of code to get v8bench restrict mode clean, i.e. have it complete without throwing any restrict mode exceptions. That’s 8 lines out of 11208, or 0.07%! 99.93% was already valid restrict mode. It’s a step in the right direction in verifying that the restrict mode subset of JavaScript is sane and well enough matches the JS subset people already use in practice.

Oh, and if the same procedure is applied to r2537 instead of HEAD, one defect in the splay benchmark (converting the same numeric key to a string over and over again) stands out and is easily corrected. That defect was later fixed in version 6 of the benchmark. Can restrict mode help you catch similar or other defects in your program?

To learn more about restrict mode read the case for restrict mode and then visit restrictmode.org.

Here’s the full diff:

Index: run.js
===================================================================
--- run.js (revision 7463)
+++ run.js (working copy)
@@ -38,7 +38,7 @@
 var success = true;
  
 function PrintResult(name, result) {
-  print(name + ': ' + result);
+  print(name + ': ' + String(result));
 }
  
  
Index: raytrace.js
===================================================================
--- raytrace.js (revision 7463)
+++ raytrace.js (working copy)
@@ -617,8 +617,8 @@
                 rayDepth: 2
             }, options || {});
  
-        this.options.canvasHeight /= this.options.pixelHeight;
-        this.options.canvasWidth /= this.options.pixelWidth;
+        this.options.canvasHeight /= Number(this.options.pixelHeight);
+        this.options.canvasWidth /= Number(this.options.pixelWidth);
  
         /* TODO: dynamically include other scripts */
     },
Index: base.js
===================================================================
--- base.js (revision 7463)
+++ base.js (working copy)
@@ -201,10 +201,10 @@
 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) {
   function Measure(data) {
     var elapsed = 0;
-    var start = new Date();
+    var start = Number(new Date());
     for (var n = 0; elapsed < 1000; n++) {
       benchmark.run();
-      elapsed = new Date() - start;
+      elapsed = Number(new Date()) - start;
     }
     if (data != null) {
       data.runs += n;
Index: deltablue.js
===================================================================
--- deltablue.js (revision 7463)
+++ deltablue.js (working copy)
@@ -800,7 +800,7 @@
  
   // Build chain of n equality constraints
   for (var i = 0; i <= n; i++) {
-    var name = "v" + i;
+    var name = "v" + String(i);
     var v = new Variable(name);
     if (prev != null)
       new EqualityConstraint(prev, v, Strength.REQUIRED);
@@ -836,8 +836,8 @@
  
   var dests = new OrderedCollection();
   for (var i = 0; i < n; i++) {
-    src = new Variable("src" + i, i);
-    dst = new Variable("dst" + i, i);
+    src = new Variable("src" + String(i), i);
+    dst = new Variable("dst" + String(i), i);
     dests.add(dst);
     new StayConstraint(src, Strength.NORMAL);
     new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);

Show comments (reddit)


Follow me on Twitter

« C-style assertions in JavaScript via JSShaper    ↑ Home     »