Hi everyone,
In our app, we have a key routine that returns defensive copies of its
output value. I have included a version of the copy function that this
routine uses below. Measuring this routine suggested that it was
responsible for up to 25% of our total run time.

On a hunch, I ported this function to java. It appears to run roughly
20 times faster there(!) I have included a version of the java
function below as well, hoping that someone on this list may find it
useful.

My question though: is this expected? Why is this function so much
faster in Java? Am I measuring something incorrectly? Finally, does
the Java routine look right?

Thanks in advance.
A

/*
    Recursively copies an object or array, or returns a primitive
value.
    Doesn't handle objects with prototypes, functions.
*/
var copy = function( base ){
    if ( typeof base != "object" ) return base;
    if (base == null) return null;

    var r;
    if (msjs.isArray(base)) r = [];
    else r ={};

    if (base){
        for( var k in base ){
            r[k] = msjs.copy(base[k]);
        }
    }

    return r;
}

private static final Object[] EMPTY_LIST = {};
public Object copyObject(Object o){
    if (o == null) return null;
    if ( ! (o instanceof ScriptableObject) ) return o;
    ScriptableObject obj = (ScriptableObject) o;

    ScriptableObject copy;
    if (obj instanceof org.mozilla.javascript.NativeArray) {
        copy = makeArray(EMPTY_LIST);
    } else {
        copy = makeObject();
    }

    for (Object id : obj.getIds()){
        if (id instanceof Integer) {
            Integer key = (Integer) id;
            Object value = copyObject(obj.get(key, obj));
            copy.put(key, copy, value);
        }else{
            String key = (String) id;
            Object value = copyObject(obj.get(key, obj));
            copy.put(key, copy, value);
        }
    }

    return copy;
}
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to