Skip to content

Instantly share code, notes, and snippets.

@hns
Created April 29, 2010 20:57
Show Gist options
  • Save hns/384231 to your computer and use it in GitHub Desktop.
Save hns/384231 to your computer and use it in GitHub Desktop.
An early glimpse on rhino performance improvements i'm working on
// 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
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