Streamlined [JavaScript?] solution for displaying and modifying several location-specific purchasing and selling rates

No, this is not related to interviews or cryptocurrencies! :)
It is for a non-profit personal web application designed to enhance a game.

This question involves both finance and coding.
I am developing this web app using Vue.js, so I prefer a JavaScript solution. However, an abstract approach or pseudo-code would also be helpful as I can translate it into JavaScript.

Here is the scenario:
Multiple locations (referred to as "stores") buy and sell various items at different prices.
Prices fluctuate randomly, possibly due to supply and demand, but that is not crucial to the problem.
There will be around 100-1000 locations and 10-100 different items.
The majority of locations (estimated 80-90%) will only deal with a few items.
Some locations (about 1-10%) will handle many items, if not all.
Around 50% of locations may only buy or sell, not both.

The objective is to display an extensive list of all locations that have a buying price for an item compared to locations that have a selling price for that same item (similar to permutations/combinations, but with expected gaps).
The final output should be a simple list/array of objects (keyed/value pairs).

A worst-case scenario could involve a paginated list of 99 million objects (1000 stores * (1000 - 1) stores * 100 items); however, there are anticipated gaps in the data, so ideally there would be significantly fewer than 1 million objects (ideally under 100,000; they do not all need to be live in memory and could be loaded from storage).

For example (sorted by Item and Buy Location for clarity):


Item     Buy Location   Buy Price Ratio Sell Price   Sell Location
Item A   Location A     4         0.5   2            Location B
Item A   Location A     4         2.0   8            Location C
Item A   Location B     6         0.66  4            Location A
Item A   Location B     6         1.33  8            Location C
Item A   Location C     10        0.4   4            Location A
Item A   Location C     10        0.2   2            Location B
Item B   Location A     3         0.33  1            Location B
Item B   Location A     3         1.33  4            Location C
Item B   Location B     3         0.66  2            Location A
Item B   Location B     3         1.33  4            Location C
Item B   Location C     5         0.4   2            Location A
Item B   Location C     5         0.2   1            Location B

Each column in the list is sortable, with the default being "Ratio" to display the optimal combinations of buying and selling locations. When prices are updated, Vue.js automatically updates and resorts the list.

Currently, I have inefficient ideas on how to tackle this issue, which involve regenerating the entire list whenever a single price changes.
Since the final output must support arbitrary sorting, direct indexing or access to items is not plausible.
If the price for a specific location's item changes, there isn't a method to pinpoint and update that specific row or cell.

I suspect that my approach is lacking efficiency, and I believe this task has been addressed before in scalable ways.
For instance, creating a straightforward buy/sell price chart across multiple exchanges.
While considering exploring open-source cryptocurrency bot code, this seems more like a finance-related challenge that I wish to comprehend rather than a programming one that I intend to replicate.

How would you go about implementing this?
What kind of dataset(s) would you utilize?

Thank you!

Answer №1

In my perspective

  • The 1 million lines of data may overwhelm users if presented all at once.
  • While a 1 million array size may not be too large for Javascript (~10MB, as demonstrated in your application here), rendering it all to the DOM element could pose significant challenges.

How would you tackle this issue?

In such a scenario, I would opt for pagination or aggregation (e.g., displaying the first 10 highest ratios for each item).

For instance:

computed: {
  displayItems () {
    // This will get updated when rawItems, currentPage, or pageSize change
    return doFilterOrSort(rawItems).slice(currentPage * pageSize, pageSize)
  }
}

This approach addresses the problem of an excessive number of DOM elements (assuming that updating a 1 million array size is fast enough not to become a bottleneck).

If your data changes frequently, like 10 times in a second, consider throttling or debouncing to reduce rendering frequency.

Example of throttling:

watch: {
  rawItems () {
    if (!ticking) {
      requestAnimationFrame(() => {
        displayItems = doFilterOrSort(rawItems).slice(currentPage * pageSize, pageSize)
        ticking = false
      })
      ticking = true
    }
  }
}

If you wish to implement infinite scrolling, recycle out-of-screen DOM elements. Refer to examples in react-virtualized.

What type of dataset(s) should you use?

Without details on the data format retrieved (initially and with updates), it's challenging to provide a precise answer.

Assuming a fixed number of stores and items with updates per item, I suggest using three arrays:

1. The first array stores buy/sell values for each item in every store. Updates here are easily done in O(1) time complexity.

2. The second array holds ratio objects for combinations of store-item pairs. Updating this incurs O(n⋅n) operations, e.g., updating 'ab' and 'ba' for a set of 3 stores and 1 item.

3. The third array extends the previous one by incorporating sorting or grouping, costing O((n⋅n⋅m) log (n⋅n⋋m)) (a potential bottleneck requiring careful attention based on array structure).

In case these solutions are insufficient, consider aggregating data on the server before transmitting it via web sockets instead of sending everything to the client side.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving data in [slug].js using Reactjs

I am currently working on a project in Reactjs utilizing the "nextjs" framework. I have successfully managed to retrieve data (specific blog details) based on the slug([slug.js]). However, I now need to display data from all other blogs within the same c ...

identifying which specific button was clicked using JavaScript/jQuery

All of the buttons on my page are identical - same title, same value, everything is the same. Is there a way for me to identify which specific button was clicked? ...

Troubleshooting Problems with Deploying Next Js on Firebase

I am currently working on a new Next Js application and have successfully deployed it on Vercel by linking the GitLab project. Now, I need to deploy the same project on Firebase. Here's what I have tried so far: - Ran firebase init This command gen ...

After manipulating the array, Vue fails to render the input fields generated by the v-for directive

After setting the value externally, Vue component won't re-render array items. The state changes but v-for element does not reflect these changes. I have a component that displays items from an array. There are buttons to adjust the array length - &a ...

The continuous firing of the postback event of the Asp.Net Button is

Why does the postback event keep firing for my button? Strangely, when I try to debug with Firebug and set a break point on the function(e) part, the code seems to skip right over it. Even using return false doesn't seem to resolve the issue. <sc ...

The Bootstrap modal stubbornly refuses to close even after resetting the HTML body

I am having an issue with my bootstrap modal where the closeModal button is not functioning properly after the printModal button has been clicked. The modal does not close as expected. Step 1: Click on the printModal button after the modal pops up (this w ...

Understanding how to retrieve a particular list item in JQuery without having the index in advance

I have a lengthy list that is broken down into various subheadings. How can I retrieve the second to last element of the list before a specific subheading, provided it is not the final element? Is it possible to do this if I know the ID of the subheading? ...

Javascript encountering issues with recognizing 'self.function' within an onclick event

Recently, I have been working on enhancing a Javascript file that is part of a Twitter plugin. One of the key additions I made was implementing a filter function for this plugin. Here is a snippet of the script showcasing the relevant parts: ;(function ( ...

Encountered difficulty accessing the controller ActionResult from JavaScript代码

Resolution: After thorough investigation, I successfully identified and resolved the issue. Surprisingly, it was not related to the Javascript or Controller code as initially anticipated. The root cause stemmed from a .dll file that was causing discrepanci ...

Mobile-responsive iFrame content is designed to adjust and fit appropriately on

While my iframe is responsive on both mobile and website, I am facing an issue where the content overflows (both overflow X and Y) from the width and height of the iFrame. Here's the scenario: in the mobile view (simulated using Google Chrome to mimi ...

What is the best way to eliminate duplicate values from an Array in ReactJS?

Hi there, I'm new to JavaScript and React. I need some help with a project I found on the React blog. I want to try solving it in my own way. This is the content of todoList.js: const todoList = [ {category: 'Sporting Goods', price: &a ...

Uploading directly to AWS S3: SignatureDoesNotMatch error specifically for Internet Explorer users

My process involves using Amazon Web Service S3 for uploading and storing files. I create a pre-signed URL using the AWS SDK for Node.js on the server-side to enable direct file uploads from the browser through this signature URL. The Process On the serv ...

The controller in ng-controller returns an undefined error when loaded using RequireJs

I have encountered an issue while loading the Angular application using requirejs. The ng-controller written in main.html loads directly but ends up being undefined. app.js define(['sfongApp', 'ng-grid', 'angular-bootstrap'] ...

Having difficulty with a script not functioning properly within an onclick button

In my script, I am using the following code: for (var i in $scope.hulls) { if ($scope.hulls[i].id == 1234) { console.log($scope.hulls[i]); $scope.selectedHullShip1 = $scope.hulls[i]; } } The code works fine outside of the onclick button, but fails to run ...

What steps should I take to address the issue of fixing the classname rather than using the

<div ng-class:"{{myclass}}" role="progressbar" aria-valuenow="{{roundedtotalPerformanceCount}}" aria-valuemin="0" aria-valuemax="100" ng-style="{'width' : ( totalPerformanceCount + '%' ) }"> {{roundedtotalPerformanceCou ...

Ensuring maximum length validation on input

I am having an issue with the function "checkNumbers()". I am attempting to validate if my input does not exceed 10 numbers; while "isAllowedSymbol()" is functioning correctly, the other one is not. What could be causing this problem? function isAllowe ...

How come my date computed property does not update reactively when changes occur?

I have a Date object in my data, and I need to convert the date into a string for a date picker component in Vuetify. The initial date is being read and displayed correctly. I am able to set the date as well - when I set a code breakpoint, I can see the ...

Customizing the placeholder font size in Material UI Autocomplete using ReactJS

Is there a way to change the placeholder font size for Material UI Autocomplete? https://i.stack.imgur.com/x71k2.png <Autocomplete multiple id="tags-outlined" options={top100F ...

How to transfer data from an HTML form to PHP using AJAX

I've encountered an issue while working on a simple application that consists of one HTML file communicating with a PHP page using AJAX. Take a look at my index.html below: <!DOCTYPE html> <html><head> <meta charset="utf-8"> & ...

Converting JSON data into clickable URL links and retrieving information upon clicking

I have a dataset in JSON format. As I iterate through it, I'm inserting selected values into an HTML link element as shown below: getPatchList: function() { $.ajax({ url: "/returneddata" }).done(function(r ...