Simple example demonstrating the speed of O(N²), O(N), O(log(N)), O(1) empirically.
// ES5 compliant.
window.onload = function () {
var quadratic = function() {
var BEGIN = new Date();
for (let i = 0; i < 10000; i++) {
for (let j = 0; j < 10000; j++) {
}
}
var END = new Date();
print_console(`Time taken: ${END - BEGIN}`, 'quadratic');
};
var linear = function() {
const BEGIN = new Date();
for (let i = 0; i < 100000; i++) {}
const END = new Date();
print_console(`Time taken: ${END - BEGIN}`, 'linear');
};
var logarithimic = function() {
const BEGIN = new Date();
for (let i = 0; i < 100000; i++) {
for (let j = 0; j < 100000; j++) {
j = j * 2;
}
}
var END = new Date();
print_console(`Time taken: ${END - BEGIN}`, 'logarithimic');
};
var constant =function() {
var BEGIN = new Date();
var END = new Date();
print_console(`Time taken: ${END - BEGIN}`, 'constant');
};
quadratic();
linear();
logarithimic();
constant();
};