Does SSJS have performance when using @Functions?

If I want to parse a text box in SSJS, there are two main tools. Built-in JavaScript code and newly converted @Functions. Are @Functions slower than using pure javascript? Or is there no real difference?

viewScope.put("length", tmpStr.length) 

against.

 viewScope.put("length:, @Length(tmpStr)) 
+4
source share
5 answers

It seems to me that @Formula does not work as fast as using SSJS code.

You can easily test yourself with some code like this (reload the page several times to get serious results):

 <?xml version="1.0" encoding="UTF-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core"> <xp:label id="label1"> <xp:this.value> <![CDATA[#{javascript: var start = java.lang.System.currentTimeMillis(); var testString = "0123456789"; var dummy; for( var i=0; i<100000; i++ ){ dummy = @Length( testString ) } var stop = java.lang.System.currentTimeMillis(); stop - start + " ms"}]]> </xp:this.value> </xp:label> <xp:br></xp:br> <xp:br></xp:br> <xp:label id="label2"> <xp:this.value> <![CDATA[#{javascript: var start = java.lang.System.currentTimeMillis(); var testString = "0123456789"; var dummy; for( var i=0; i<100000; i++ ){ dummy = testString.length; } var stop = java.lang.System.currentTimeMillis(); stop - start + " ms"}]]> </xp:this.value> </xp:label> </xp:view> 
+4
source

All SSJS are parsed in AST (abstract syntax tree) at runtime. In other words, your code simply remains a string until it is executed, and at that moment the parser checks that String syntactically determines what the code contains: what characters denote variables that are operators, functions, etc. After this parsing is complete, the execution engine can run Java code, which is an approximation of what was developed for JavaScript code.

This is why SSJS is always slower than directly equivalent to Java: if you are just starting to write your code in Java, then it is compiled into bytecode at the time your project was created, but perhaps there’s no more need to “guess” at runtime which run the code by analyzing String ... it just runs the already defined Java code.

On the other hand, there is nothing about this process that would significantly distinguish SSJS implementation of various @Functions from native JavaScript; given that @Length (tmpStr) is just a wrapper for tmpStr.length, it doesn’t surprise me that Sven sees the difference in runtime, given the sufficient number of iterations. But if your goal is optimization, you will get much more improvement by moving all the code from SSJS blocks to bean methods than you, avoiding the convenience of @Functions in favor of native JavaScript, because even native JavaScript needs to be analyzed in AST. In this sense, there is no fundamental difference between the two.

UPDATE: There is a slight caveat to the AST analysis mentioned at the beginning of this answer. By default, the XPages runtime caches up to 400 unique SSJS expressions (you can override this restriction using the ibm.jscript.cachesize property in the xsp.properties server file). Therefore, if there is an expression that exactly matches (including a space) that is already cached, Domino does not need to create a new AST for this expression; it simply refers to a tree already in the cache. This is the MRU cache ("most recently used"), so the more often the same expression is found, the more likely it will remain in the cache. Regardless of whether the AST is cached, it still needs to be evaluated against the current context, and some of the JavaScript wrapper objects have additional overhead compared to what you are most likely to use if you just encoded directly on Java (for example, {} becomes an ObjectObject , which is similar to HashMap , but has additional functions that support closure, which are simply lost if you do not use closure anyway). But the main characteristic of this AST cache is that, unlike most development contexts, code duplication can really be good, if only in the sense that using the same exact expression again and again allows you to use everything except the first instance each skip language parsing and go straight to the challenge.

+16
source

I don't think that @formula in SSJS is as fast as the traditional @formula. One reason would be that @formula is just another layer on top of SSJS functionality, and therefore there is still some code to execute.

But this is just a wild hunch.

+1
source

As for @DBLookup, I did some testing and found that it is significantly slower than getting a view in SSJS and then creating getDocumentByKey and getting fields from the found NotesDocument. At least 10 times slower when I looped four @DBLookups 100 times against getting the document 100 times, and then got four fields.

Howard

+1
source

tmpStr.length is a built-in browser function (not in Javascript, but the code inside the browser is compiled)

@Length (tmpStr) is a javascript function, so it is interpreted by the browser

0
source

Source: https://habr.com/ru/post/1402873/


All Articles