Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance loss when using HashMap.put method in SSJS

In my XPages applications I frequently use SSJS objects (com.ibm.jscript.std.ObjectObject) to cache data. To check if the performance improves if I use java.util.HashMaps instead, I benchmarked the execution times of the following code snippets (SSJS):

SSJS code snippets that were benchmarked

All three code snippets do the same: they create and fill either a SSJS Object or a HashMap with different types of data / objects. For each of the snippets I measured the average execution times over 1000 runs, with n (=maximum loop index in the snippet) being 1000000 (1 million). The benchmarks were performed on a Domino 9.0.1 server using java.lang.System.nanoTime .

The ratios of the execution times are as follows:

  • 154% for T[HashMap] / T[SSJS Object]
  • 266% for T[HashMap with put method] / T[SSJS Object]
  • 172% for T[HashMap with put method] / T[HashMap]

In other words:

  • filling a HashMap took ~54% longer than filling a SSJS object
  • filling a HashMap using the put method took ~166% longer than filling a SSJS object
  • filling a HashMap using the put method took ~72% longer than filling a HashMap using the SSJS "." notation

My questions are as follows:

  1. I frequently see SSJS code where HashMaps are used to store data. Why not use the standard SSJS objects if they provide better performance?
  2. Why is it so much more inefficient to use the put method instead of the SSJS "." notation to set the value of a HashMap?
like image 588
xpages-noob Avatar asked Feb 11 '23 04:02

xpages-noob


1 Answers

Looking at your code (you should put code there, not an image), you are comparing apples and oranges.

  oo = {};

initializes the JS object once, in the second loop it overwrites the existing object.

 hw = new HashMap();

generates a new object each time and throws the old one to the garbage collector. That will be slower.

Your conclusion: "Filling a Hashmap took xx longer" is not supported by your data. The code you wrote supports "Creating and discarding HashMaps in quick sequence is slower".

If you want to run a reasonable test you need to:

  • Create ONE object (that's the usual use case) and put "a lot of things" into it and to get real ideas: put 100,000 things in. Then read 100,000, then update 100,000 things.

The main reason for HashMaps: you can use them in Java too and you can't accidentally add functionality to them as you can with a JavaScript object. The dot vs. put conversion is due to the different type conversion mechanism.

like image 146
stwissel Avatar answered Mar 04 '23 20:03

stwissel