Skip to content

Commit d61e681

Browse files
vaadin-bottltv
andauthored
fix: optimize Element API for small datasets (#22196) (#22246)
Reduces usage of Stream API in executeJs and callJsFunction methods for improved performance with small datasets. These methods are usually used with one to few objects in the dataset, they can be called a lot depending on a number of active sessions. This change replaces Stream API usage with Array API, Arrays.copyOf, and System.arraycopy. Fixes: #21874 Co-authored-by: Tomi Virtanen <tltv@vaadin.com>
1 parent fefa572 commit d61e681

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

flow-server/src/main/java/com/vaadin/flow/dom/Element.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.vaadin.flow.dom;
1717

1818
import java.io.Serializable;
19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Locale;
@@ -1370,8 +1371,14 @@ public PendingJavaScriptResult callJsFunction(String functionName,
13701371
String paramPlaceholderString = IntStream.range(1, arguments.length + 1)
13711372
.mapToObj(i -> "$" + i).collect(Collectors.joining(","));
13721373
// Inject the element as $0
1373-
Stream<Serializable> jsParameters = Stream.concat(Stream.of(this),
1374-
Stream.of(arguments));
1374+
Serializable[] jsParameters;
1375+
if (arguments.length == 0) {
1376+
jsParameters = new Serializable[] { this };
1377+
} else {
1378+
jsParameters = new Serializable[arguments.length + 1];
1379+
jsParameters[0] = this;
1380+
System.arraycopy(arguments, 0, jsParameters, 1, arguments.length);
1381+
}
13751382

13761383
return scheduleJavaScriptInvocation("return $0." + functionName + "("
13771384
+ paramPlaceholderString + ")", jsParameters);
@@ -1424,8 +1431,14 @@ public PendingJavaScriptResult executeJs(String expression,
14241431
Serializable... parameters) {
14251432

14261433
// Add "this" as the last parameter
1427-
Stream<Serializable> wrappedParameters = Stream
1428-
.concat(Stream.of(parameters), Stream.of(this));
1434+
Serializable[] wrappedParameters;
1435+
if (parameters.length == 0) {
1436+
wrappedParameters = new Serializable[] { this };
1437+
} else {
1438+
wrappedParameters = Arrays.copyOf(parameters,
1439+
parameters.length + 1);
1440+
wrappedParameters[parameters.length] = this;
1441+
}
14291442

14301443
// Wrap in a function that is applied with last parameter as "this"
14311444
String wrappedExpression = "return (async function() { " + expression
@@ -1436,11 +1449,11 @@ public PendingJavaScriptResult executeJs(String expression,
14361449
}
14371450

14381451
private PendingJavaScriptResult scheduleJavaScriptInvocation(
1439-
String expression, Stream<Serializable> parameters) {
1452+
String expression, Serializable[] parameters) {
14401453
StateNode node = getNode();
14411454

14421455
JavaScriptInvocation invocation = new JavaScriptInvocation(expression,
1443-
parameters.toArray(Serializable[]::new));
1456+
parameters);
14441457

14451458
PendingJavaScriptInvocation pending = new PendingJavaScriptInvocation(
14461459
node, invocation);

0 commit comments

Comments
 (0)