Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Created September 17, 2019 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatron/1aa4dc43357a2c449347b73cb35ce90d to your computer and use it in GitHub Desktop.
Save jaredatron/1aa4dc43357a2c449347b73cb35ce90d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
function sortSortedWithArrayManipulation(a, b){
a = [...a]
b = [...b]
const sorted = []
while (a.length || b.length){
if (a.length === 0){
sorted.push(...b)
break
}else if (b.length === 0){
sorted.push(...a)
break
}else if (a[0] < b[0]){
sorted.push(a.shift())
}else if (a[0] > b[0]){
sorted.push(b.shift())
}else{
sorted.push(a.shift())
sorted.push(b.shift())
}
}
return sorted
}
function sortSortedWithIndexTracking(a, b){
let aIndex = 0
let bIndex = 0
const sorted = []
while (aIndex < a.length || bIndex < b.length){
if (aIndex >= a.length){
sorted.push(...b.slice(bIndex))
break
}else if (bIndex >= b.length){
sorted.push(...a.slice(aIndex))
break
}else if (a[aIndex] < b[bIndex]){
sorted.push(a[aIndex])
aIndex++
}else if (a[aIndex] > b[bIndex]){
sorted.push(b[bIndex])
bIndex++
}else{
sorted.push(a[aIndex])
aIndex++
sorted.push(b[bIndex])
bIndex++
}
}
return sorted
}
function sortSortedByJon(a, b){
const results = [];
for (let i=0, j=0; i < a.length || j < b.length; ) {
if (i === a.length) {
results.push(b[j++]);
} else if (j === b.length) {
results.push(a[i++]);
} else if (a[i] < b[j]) {
results.push(a[i++]);
} else {
results.push(b[j++]);
}
}
return results;
}
function sortSortedByJonAndJaredA(a, b){
const results = [];
const aLength = a.length;
const bLength = b.length;
let aIndex=0;
let bIndex=0;
while(aIndex < aLength || bIndex < bLength){
if (aIndex === aLength) {
results.push(b[bIndex++]);
} else if (bIndex === bLength) {
results.push(a[aIndex++]);
} else if (a[aIndex] < b[bIndex]) {
results.push(a[aIndex++]);
} else {
results.push(b[bIndex++]);
}
}
return results;
}
function sortSortedByJonAndJaredB(a, b){
const aLength = a.length;
const bLength = b.length;
const results = new Array(aLength + bLength);
let resultsIndex = 0;
let aIndex=0;
let bIndex=0;
while(aIndex < aLength || bIndex < bLength){
if (aIndex === aLength) {
results[resultsIndex++] = b[bIndex++];
} else if (bIndex === bLength) {
results[resultsIndex++] = a[aIndex++];
} else if (a[aIndex] < b[bIndex]) {
results[resultsIndex++] = a[aIndex++];
} else {
results[resultsIndex++] = b[bIndex++];
}
}
return results;
}
function sortSortedByJonAndJaredC(a, b){
const aLength = a.length;
const bLength = b.length;
const resultsLength = aLength + bLength;
const results = new Array(resultsLength);
let resultsIndex = 0;
let aIndex=0;
let bIndex=0;
while(resultsIndex < resultsLength){
if (aIndex === aLength) {
results[resultsIndex++] = b[bIndex++];
} else if (bIndex === bLength) {
results[resultsIndex++] = a[aIndex++];
} else if (a[aIndex] < b[bIndex]) {
results[resultsIndex++] = a[aIndex++];
} else {
results[resultsIndex++] = b[bIndex++];
}
}
return results;
}
function sortSortedByJonAndJaredD(a, b){
const aLength = a.length;
const bLength = b.length;
const resultsLength = aLength + bLength;
const results = new Array(resultsLength);
let resultsIndex = 0, aIndex = 0, bIndex = 0;
while (resultsIndex < resultsLength) results[resultsIndex++] = (
bIndex === bLength || a[aIndex] < b[bIndex] ? a[aIndex++] : b[bIndex++]
)
return results;
}
const CONTENDORS = {
sortSortedWithArrayManipulation,
sortSortedWithIndexTracking,
sortSortedByJon,
sortSortedByJonAndJaredA,
sortSortedByJonAndJaredB,
sortSortedByJonAndJaredC,
sortSortedByJonAndJaredD,
}
function expectDeepEqual(a, b){
a = JSON.stringify(a)
b = JSON.stringify(b)
if (a === b) return true
throw new Error(`expected ${a} to deep equal ${b}`)
}
function hugeArray(){
return Array.from({length: 2048}, () => (Math.random())).sort()
}
const fruits = ["apple", "berry", "cherry"];
const veggies = ["artichoke", "asparagus", "celery"];
const hugeA = hugeArray().sort()
const hugeB = hugeArray().sort()
const hugeAAndhugeB = [...hugeA, ...hugeB].sort()
function tests(sortSorted){
expectDeepEqual(
sortSorted(fruits, veggies),
["apple", "artichoke", "asparagus", "berry", "celery", "cherry"],
)
expectDeepEqual(
sortSorted(veggies, fruits),
["apple", "artichoke", "asparagus", "berry", "celery", "cherry"],
)
expectDeepEqual(
sortSorted(fruits, []),
fruits,
)
expectDeepEqual(
sortSorted([], fruits),
fruits,
)
expectDeepEqual(
sortSorted(
['l', 'p', 'z'],
['a','q'],
),
['a','l','p','q','z'],
)
expectDeepEqual(
sortSorted(
hugeA,
hugeB,
),
hugeAAndhugeB,
)
}
console.log('testing…')
for(const name in CONTENDORS) {
tests(CONTENDORS[name])
console.log(`${name} PASS`)
}
// benchmarks
console.log('Benchmarking…')
const Benchmark = require('benchmark');
const BenchmarkSuite = new Benchmark.Suite;
for(const name in CONTENDORS) {
BenchmarkSuite.add(name, function(){
tests(CONTENDORS[name])
})
}
BenchmarkSuite
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': false });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment