The window() function performs 2 threshold operations (high and low). But the expensive part is performing the counting, and that only needs to be done once.
The threshold counting functions can be re-used by the window() function for a single-pass implementation.