check length beforehand to improve isEmpty perf