Let stopwatch use window.performance.now when available. Fix for: http://code.google.com/p/dart/issues/detail?id=4312

BUG=4312

Review URL: https://codereview.chromium.org//11417110

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@15343 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart b/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart
index 21cf6e1..38b90d1 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart
@@ -148,8 +148,8 @@
 
 // Patch for Stopwatch implementation.
 patch class _StopwatchImpl {
-  patch static int _frequency() => 1000;
-  patch static int _now() => Primitives.dateNow();
+  patch static int _frequency() => 1000000;
+  patch static int _now() => Primitives.numMicroseconds();
 }
 
 
diff --git a/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart b/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart
index 161ebf1..ad8d98c 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/js_helper.dart
@@ -535,6 +535,17 @@
 
   static num dateNow() => JS('num', r'Date.now()');
 
+  static num numMicroseconds() {
+    if (JS('bool', 'typeof window != "undefined" && window !== null')) {
+      var performance = JS('var', 'window.performance');
+      if (performance != null && 
+          JS('bool', 'typeof #.webkitNow == "function"', performance)) {
+        return JS('num', '#.webkitNow()', performance);
+      }
+    }
+    return 1000 * dateNow();
+  }
+
   // This is to avoid stack overflows due to very large argument arrays in
   // apply().  It fixes http://dartbug.com/6919
   static String _fromCharCodeApply(List<int> array) {