Second post here and this one's really got me scratching my head. I have a function which processes an array to try to find similar data. The array contains 1410 elements which I consider to be a lot but nothing that Node or my computer shouldn't be able to handle.
My code is giving "Segmentation Fault: 11" error which I found was to do with memory access issues so I even want as far as to test my Mac's RAM but everything's fine. The segfault makes it very difficult to debug which is why I came here.
The code where something is going wrong is within here:
return matchings.map(matchArray => {
const namesList = matchArray
.map(matchItem => matchItem.name)
.sort((a, b) => a.localeCompare(b))
const symbolsList = matchArray
.map(matchItem => matchItem.symbol)
.sort((a, b) => a.localeCompare(b))
return {
name: common.getMode(namesList),
symbol: common.getMode(symbolsList),
matches: matchArray
}
}).sort((a, b) => a.name.localeCompare(b.name))
Where matchings
is the array I'm talking about. common.getMode(array)
has this code:
array.sort()
const stats = {
top: {
name: '',
freq: 0
},
current: {
name: array[0],
freq: 1
}
}
for (let idxName = 1; idxName < array.length; idxName++) {
const currentName = array[idxName]
const lastName = array[idxName - 1]
if (currentName === lastName) {
stats.current.freq++
} else {
if (stats.current.freq > stats.top.freq) {
stats.top.name = stats.current.name
stats.top.freq = stats.current.freq
}
stats.current = {
name: currentName,
freq: 1
}
}
}
if (stats.current.freq > stats.top.freq) {
stats.top.name = stats.current.name
stats.top.freq = stats.current.freq
}
return stats.top.name
It's worth mentioning that when performed with an array of smaller size ~1000, the code works fine which leads me to believe it is not my code. There is also very little content online about Node's Segfault 11 which isn't helping.
Any ideas greatly appreciated!