# Speed up Katalon String operations

**URL:** <https://forum.katalon.com/t/speed-up-katalon-string-operations/66505>\
**Category:** Miscellaneous\
**Created:** [July 1, 2022, 8:07pm UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505 "2022-07-01T20:07:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![em817m](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/em817m/32/20464_2.png) [@em817m](https://forum.katalon.com/u/em817m)\
**Post date:** [July 1, 2022, 8:07pm UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505/1 "2022-07-01T20:07:43Z")

</div>

I have a situation where I have to build a string that is used as input to a UI Text Area. There are 235K lines of data that are read from a flat file, put into a list and then read out, one-by-one and appended with a new line character and then put into a string.

The reading from a flat file and creating the list is reasonable within Katalon. But it’s taking 25 minutes to create the string. The entire Test Case takes 31 minutes to run so I’ve been asked to look into this issue.

My code is here:

```auto
//searchList holds the 235K lines of raw input data from the flat file
int searchListSize = searchList.size()
String mispSearchString = ''
for (int row = 0; row < searchListSize; row++) {
	if ((row + 1) == searchListSize) {
		mispSearchString = (mispSearchString + searchList.get(row))
	} else {
		mispSearchString = ((mispSearchString + searchList.get(row)) + '\\n')
	}
}

```

Everything works, it’s just taking too long.

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [July 1, 2022, 10:13pm UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505/3 "2022-07-01T22:13:02Z")

</div>

Doesn’t the flat file already include newline chars? If it does…

Don’t read it into a list, just read the file into a string:

```javascript
String readFileIntoString(String filepath) {
  File file = new File(filepath)
  return file.text
}

```

Hopefully, that will retain the existing `\n` chars

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 2, 2022, 12:41am UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505/4 "2022-07-02T00:41:04Z")

</div>

@em817m

Do you know programming terms: an object is `mutable` or `immutable`?

`java.lang.String` class is immutable. You are concatenating 2 strings with the `+` operator to produce a new String object. Your code will repeat instantiating a new String object and de-referencing the previous String object for 235K times. The String objects will consume much memory space, creating objects consumes CPU power. It is a bad practice repeating String concatenation using `+` operator.

There is a Java Class `java.lang.StringBuilder`, which is mutable. It is designed to perform string concatenation efficiently. Have a look at some article, for example;

- [https://www.baeldung.com/java-strings-concatenation](https://www.baeldung.com/java-strings-concatenation)
- [https://www.baeldung.com/java-string-builder-string-buffer](https://www.baeldung.com/java-string-builder-string-buffer)

The following sample shows how to use StringBuilder.

```auto
List<String> searchList = ["aaa", "bbb", "ccc"]

int searchListSize = searchList.size()
StringBuilder buf = new StringBuilder()
for (int i= 0; i < searchListSize; i++) {
	if (buf.size() > 0) {
		buf.append('\n')
	}
	buf.append(searchList[i])
}

println ">>>" + buf.toString() + "<<<"

```

Output:

```auto
>>>aaa
bbb
ccc<<<

```

You can swap the `searchList` to have 235K lines. StringBuilder would work in seconds.

---

<div class="post-metadata">

**Author:** ![em817m](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/em817m/32/20464_2.png) [@em817m](https://forum.katalon.com/u/em817m)\
**Post date:** [July 5, 2022, 2:28pm UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505/5 "2022-07-05T14:28:57Z")

</div>

kazurayam,  
Thank you for your suggestion & example. Your code brought the load time down to 1 second from 25 minutes.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 5, 2022, 2:42pm UTC](https://forum.katalon.com/t/speed-up-katalon-string-operations/66505/6 "2022-07-05T14:42:05Z")

</div>

Closed on July 5, 2022, 2:42pm UTC


