Created
April 29, 2010 20:57
-
-
Save hns/384231 to your computer and use it in GitHub Desktop.
An early glimpse on rhino performance improvements i'm working on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// an early glimpse on rhino performance improvements i'm working on. | |
// this will require both the native-callsites and non-object-this branches | |
// on http://github.com/hns/rhino-8 to be applicable to vanilla strings | |
>> defineClass(org.ringojs.wrappers.FastString) | |
>> var fast = new FastString("foo") | |
>> var str = "foo" | |
>> var timer = require("ringo/utils").timer | |
>> timer(function() {for (var i = 0; i < 10000000; i++) str.indexOf("f");}) | |
7720 millis | |
>> timer(function() {for (var i = 0; i < 10000000; i++) fast.indexOf("f");}) | |
928 millis | |
>> timer(function() {for (var i = 0; i < 10000000; i++) str.toUpperCase();}) | |
8447 millis | |
>> timer(function() {for (var i = 0; i < 10000000; i++) fast.toUpperCase();}) | |
1587 millis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.ringojs.wrappers; | |
import org.mozilla.javascript.Context; | |
import org.mozilla.javascript.Function; | |
import org.mozilla.javascript.Scriptable; | |
import org.mozilla.javascript.ScriptableObject; | |
import org.mozilla.javascript.annotations.JSFunction; | |
import org.mozilla.javascript.annotations.JSStaticFunction; | |
// Note: there's nothing special about this host class (except for the missing | |
// input validation). I'm just including it for the sake of completeness. The | |
// performance gains stem from the native-callsites branch at | |
// <http://github.com/hns/rhino-8>, and the non-object-this branch should transfer | |
// that to string and number methods. | |
// See <http://wiki.github.com/hns/rhino-8/> for more information. | |
public class FastString extends ScriptableObject { | |
private String string; | |
public FastString() {} | |
public FastString(String string) { | |
this.string = string; | |
} | |
public String getClassName() { | |
return "FastString"; | |
} | |
@JSFunction | |
@JSStaticFunction | |
public static Object indexOf(Context cx, Scriptable thisObj, | |
Object[] args, Function funObj) { | |
String str, pattern; | |
if (thisObj == null) { | |
str = args[0].toString(); | |
pattern = args[1].toString(); | |
} else { | |
str = thisObj.toString(); | |
pattern = args[0].toString(); | |
} | |
return Integer.valueOf(str.indexOf(pattern)); | |
} | |
@JSFunction | |
public String toUpperCase() { | |
return string.toUpperCase(); | |
} | |
@Override | |
@JSFunction | |
public String toString() { | |
return this.string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment