Тест список vs массив

Benchmark created on


Setup

function createLinkedList(length) {
    if (length <= 0) {
        return null;
    }
    
    // Возможные символы и направления
    const symbols = ["SBER_TQBR", "GAZP_TQBR", "LKOH_TQBR", "YNDX_TQBR", "ROSN_TQBR"];
    const directions = ["BUY", "SELL"];
    
    let head = null;
    let current = null;
    let baseTimestamp = Date.now() * 1000; // Начальный timestamp в микросекундах
    
    for (let i = 0; i < length; i++) {
        // Генерируем случайные данные
        const symbol = symbols[Math.floor(Math.random() * symbols.length)];
        const direction = directions[Math.floor(Math.random() * directions.length)];
        const price = +(Math.random() * 500 + 100).toFixed(2); // Цена от 100 до 600
        const quantity = Math.floor(Math.random() * 50) + 1; // Количество от 1 до 50
        const timestamp = baseTimestamp + i * 1000000; // Увеличиваем на 1 секунду каждый раз
        
        // Создаем новый узел
        const node = {
            s: symbol,
            d: direction,
            p: price,
            q: quantity,
            ts: timestamp,
            src: "t",
            seq: timestamp,
            next: null
        };
        
        // Если это первый узел
        if (head === null) {
            head = node;
            current = node;
        } else {
            // Присоединяем к цепочке
            current.next = node;
            current = node;
        }
    }
    
    return head;
}

function createTradeArray(length) {
    if (length <= 0) {
        return [];
    }
    
    // Возможные символы и направления
    const symbols = ["SBER_TQBR", "GAZP_TQBR", "LKOH_TQBR", "YNDX_TQBR", "ROSN_TQBR"];
    const directions = ["BUY", "SELL"];
    
    const trades = [];
    let baseTimestamp = Date.now() * 1000; // Начальный timestamp в микросекундах
    
    for (let i = 0; i < length; i++) {
        // Генерируем случайные данные
        const symbol = symbols[Math.floor(Math.random() * symbols.length)];
        const direction = directions[Math.floor(Math.random() * directions.length)];
        const price = +(Math.random() * 500 + 100).toFixed(2); // Цена от 100 до 600
        const quantity = Math.floor(Math.random() * 50) + 1; // Количество от 1 до 50
        const timestamp = baseTimestamp + i * 1000000; // Увеличиваем на 1 секунду каждый раз
        
        // Создаем объект трейда (без поля next)
        const trade = {
            s: symbol,
            d: direction,
            p: price,
            q: quantity,
            ts: timestamp,
            src: "t",
            seq: timestamp
        };
        
        trades.push(trade);
    }
    
    return trades;
}


const list200 = JSON.stringify(createLinkedList(200)); 
const list500 = JSON.stringify(createLinkedList(500));
const list1000 = JSON.stringify(createLinkedList(1000));
const arr200 = JSON.stringify(createTradeArray(200));
const arr500 = JSON.stringify(createTradeArray(500));
const arr1000 = JSON.stringify(createTradeArray(1000));


function* linkedListGenerator(head) {
    let current = head;
    
    while (current) {
        yield current;
        current = current.next;
    }
}


Test runner

Ready to run.

Testing in
TestOps/sec
200 list
const list= JSON.parse(list200);
for (const node of linkedListGenerator(list)) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready
200 arr
const arr= JSON.parse(arr200);

for (const node of arr) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready
500 list
const list= JSON.parse(list500);
for (const node of linkedListGenerator(list)) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready
500 arr
const arr= JSON.parse(arr500);

for (const node of arr) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready
1000 list
const list= JSON.parse(list1000);
for (const node of linkedListGenerator(list)) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready
1000 arr
const arr= JSON.parse(arr1000);

for (const node of arr) {
    console.log(`Trade: ${node.d} ${node.q} at ${node.p}`);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.