is_empty returns true if $string is {}.
It is efficient even if $string is large binary data (see test output below).
It would be nice if there was an optimisation in the interpreter for ... eq {} or string equal to make this unnecessary.
proc is_empty {string} { expr {![binary scan $string c c]} } proc not_empty {string} { expr {![is_empty $string]} }
Update: is seems that string length $data is even faster. I guess there is a recent optimisation in string length that avoids translating the binary to a string. See output below...
Test:
foreach cmd { {expr {$data == {}}} {expr {$data eq {}}} {string bytelength $data} {string equal $data {}} {string length $data} {is_empty $data} } { set data [file get ~/stuff/file.dat] puts "$cmd\n [time {set r [eval $cmd]}]\n result = $r\n" unset data puts "" }
Output:
expr {$data == {}} 1099855 microseconds per iteration result = 0 expr {$data eq {}} 1094751 microseconds per iteration result = 0 string bytelength $data 1076724 microseconds per iteration result = 378629364 string equal $data {} 1078098 microseconds per iteration result = 0 string length $data 22 microseconds per iteration result = 191470592 is_empty $data 42 microseconds per iteration result = 0