Quantcast
Channel: ColdFusion – ColdFusion
Viewing all articles
Browse latest Browse all 100

StructCount vs StructIsEmpty

$
0
0

There was a question the other day on the CFML slack channel asking how you’d find if a struct was empty. I posted an answer that you could use StructCount to do it, someone else posted StructIsEmpty. I have to admit, I didn’t know about StructIsEmpty. This lead onto which one is more performant. When the struct is empty I’d expect there to be no difference, but what if the struct had 1,000,000 keys? I thought I’d test and post the results.

i = 0;
map = {};
while(i++ < 1000000) {
    map['key_#i#'] = i;
}

s = getTickCount();
result = StructIsEmpty(map);
e = getTickCount();
WriteDump({
    "ms": e-s,
    "result": result
});

s = getTickCount();
result = StructCount(map);
e = getTickCount();

WriteDump({
    "ms": e-s,
    "result": result
});

The outcome surprised me.

ms	0
result	NO

ms	0
result	1000000

Just for good measure, I thought I’d also try with an ordered struct (structNew("ordered")) and the result was the same. So there you go a simple question lead to an interesting investigation!

The post StructCount vs StructIsEmpty appeared first on ColdFusion.


Viewing all articles
Browse latest Browse all 100

Trending Articles