JavaScript general algorithms