Skip to content

Instantly share code, notes, and snippets.

@sammich
Last active March 31, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammich/cb616321c3699ec1d4550b0fb4260fe8 to your computer and use it in GitHub Desktop.
Save sammich/cb616321c3699ec1d4550b0fb4260fe8 to your computer and use it in GitHub Desktop.
Micro-benchmark code comparing various usages for-loop patterns vs listToNativeArray. The post is available at https://blog.samuelchan.dev/posts/working-with-ibm-bpm-lists-js/
// make a big list
tw.local.namevaluepairs = []
var testRuns = 10000,
listSize = 1
for (var i = 0; i < listSize; i++) {
tw.local.namevaluepairs.insertIntoList(0, {
name: String(i),
value: String(i)
})
}
var t1 = timer(test1)
var t1a = timer(test1a)
var t1b = timer(test1b)
var t1c = timer(test1c)
var t2 = timer(test2)
result = [t1, t1a, t1b, t1c, t2]
//
// testRuns = 1000, listSize = 100
// run 1: 844,1328,1516,2031,265
// run 2: 843,1407,1687,2094,266
// run 3: 827,1282,1531,2016,281
// testRuns = 1000, listSize = 10
// run 1: 94,125,172,203,31
// run 2: 78,125,157,203,47
// run 3: 77,141,187,203,32
// testRuns = 1000, listSize = 1
// run 1: 125,203,204,265,94
// run 2: 141,203,219,250,109
// run 3: 141,188,203,250,109
//// Note in all these tests, we are merely iterating. Nothing is returned from the lists themselves. The trivial task of
//// concatenating two strings is the trivial work being done here
// local ref to object in loop
// cache listLength
function test1() {
for (var i = 0, x = tw.local.namevaluepairs.listLength; i < x; i++) {
var hi = tw.local.namevaluepairs[i]
var blah = hi.name + ', ' + hi.value
}
}
// local ref to object in loop
// no cache listLength
function test1a() {
for (var i = 0; i < tw.local.namevaluepairs.listLength; i++) {
var hi = tw.local.namevaluepairs[i]
var blah = hi.name + ', ' + hi.value
}
}
// no local ref to object in loop
// cache listLength
function test1b() {
for (var i = 0, x = tw.local.namevaluepairs.listLength; i < x; i++) {
var blah = tw.local.namevaluepairs[i].name + ', ' + tw.local.namevaluepairs[i].value
}
}
// no local ref to object in loop
// no cache listLength
function test1c() {
for (var i = 0; i < tw.local.namevaluepairs.listLength; i++) {
var blah = tw.local.namevaluepairs[i].name + ', ' + tw.local.namevaluepairs[i].value
}
}
// native iterator
function test2() {
tw.local.namevaluepairs.listToNativeArray().forEach(function (item) {
var blah = item.name + ', ' + item.value
})
}
////////////////
function timer(fn) {
var start = Date.now()
for (var i = 0; i < testRuns; i++) {
fn()
}
return Date.now() - start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment