How to efficiently map an element's namespace in java?

The function test()

below checks if an element matches a predefined namespace NSURI

:

class MyClass {
    private static final String NSURI = "http://example.com/mynamespace";

    ...

    public test(Element e) {
        return NSURI.equals(e.getNamespaceURI());
    }
}

      

Is string comparison efficient? I have to iterate over many nodes potentially having different namespaces. But doing string comparisons every time seems wasteful.

Is there a faster way to compare namespaces?

+3


source to share


1 answer


As stated in the VGR, you are likely to come across comparisons String

with each method. However, there is one parameter that allows...

As shown here , Arrays#equals(char[], char[])

is internal, which means it calls native code (or assembly) to compare arrays, making it much faster . Keep in mind that JDK 9 will be released a week from today and this feature will change, but it will still be integral.



Thus, if you can collect each namespace into one char[]

, it may be faster to compare them this way than direct comparison String

.

It will most likely end up being overkill, but it's worth it!

0


source







All Articles