Looking for help to optimize Instagram Graph API pull for faster performance

Seeking advice on improving the performance of my code that pulls images from Instagram using the Instagram Graph API. I've encountered slow performance when using JavaScript, and even worse when using PHP. As a beginner in this area, any guidance or tips would be greatly appreciated.

Below is the current code snippet:

$.getJSON(instagram_url_1, function(data) {
    instagram_array_1 = data['data'];

    instagram_next = data['paging'];
    instagram_next = instagram_next['next'];

    for (var image_count = 0; image_count < 25; image_count++) {
      image_data = instagram_array_1[image_count];
      image_data = image_data['id'];
      image_link = 'https://graph.facebook.com/' + image_data + '?fields=media_url&access_token=' + access_token;
      image_array[image_count] = image_link;
    };

    $.getJSON(instagram_next, function(data) {
      instagram_array_2 = data['data'];

      for (image_count = 0; image_count < 25; image_count++) {
        image_data = instagram_array_2[image_count];
        image_data = image_data['id'];
        image_link = 'https://graph.facebook.com/' + image_data + '?fields=media_url&access_token=' + access_token;
        image_array[image_count + 25] = image_link;
      };

      console.log(image_array);

      for (var image_count = 0; image_count < 40; image_count++) {
        image_link_array = image_array[image_count];

        $.getJSON(image_link_array, function(data) {
          image_link = data['media_url'];
          image_html = '<img class="instagram-images" src=' + image_link + ' />';
          $('#instagram-body').append(image_html);
        });
      };
    });
  });

The website loading time could use some improvement to enhance user experience. Any suggestions are welcome! Thanks in advance!

Answer №1

  • To improve performance, avoid using this code on every page load and instead cache results server-side to prevent hitting API limits.
  • Remember not to hardcode Access Tokens on the client as they should always be kept secret. Utilize them only on the server side.
  • Optimize your API calls by fetching all images with just one call using
    /instagram-id/media?fields=media_url
    , rather than making separate calls for each image.

Reference:

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

Guide to connecting two separate components from distinct pages in React/MUI

My setup involves two pages: Appbar.js, where I have a top appbar and a Navigation Menu Icon Button that triggers the display of my Drawer (Material UI) in TempDrawer.js. Initially, everything worked fine when all the code was placed in the Appbar.js file ...

Is there a way to send an id to an ASP.Net controller via AJAX from a view?

I'm trying to pass an ID to the controller through a POST request, but it seems that no data is being sent. I am attempting to do this by triggering an Alert using Sweet Alert. Here is the code snippet for the view: @foreach (var item in Model) { ...

What is the best way to access data stored in the state of the store.js within a Vue application?

Currently, I am working on my initial project using Vue.js. The application involves a multi-step form that shares a common header and footer. As the user progresses through each step, the data entered is sent to store.js for storage. However, I have encou ...

Stop materializecss dropdown from closing when clicking within it

As I work on my current project, I am utilizing Materialize.css which includes a dropdown with various input forms inside. The dropdown can be closed by: clicking outside of .dropdown-content clicking inside of .dropdown-content clicking on .dropdown-bu ...

Issue encountered while attempting to import three.js in static context

Trying to statically import three.js for a test website. I'm a bit rusty with html/js so something seems off here. Following static import from Code below (all in one html file): <script type="module"> // Find the latest v ...

Determining Whether an Array or Linked List Forms a Palindrome

While working on a problem from Leetcode, I encountered the task of creating a function to check if an array is a palindrome. The suggested solution involved converting the array into a linked list and then checking if its elements form a palindrome sequen ...

What is the best way to eliminate the forward slash from a string?

There is a string that goes like this: var str='Dowagiac\'s Olympic wrestler recalled' The objective is to eliminate the forward slash from the string. var str1=str.replace(/\'/g, '\\\''); ale ...

Error in Angular form validation: Attempting to access property 'name' of an undefined value

Recently, I encountered an issue with my form while implementing Angular validation. The goal was to ensure that the input fields were not left blank by using an if statement. However, upon testing the form, I received the following error message: Cannot ...

What is an alternative method to reverse an array in JavaScript without using the .reverse() function?

I've been experimenting with reversing the contents of an array. My method works fine when all the elements in the array are of the same type, but as soon as I mix different types, it breaks. The caveat is that I'm not allowed to use the .rever ...

Sharing data between different sections of Jade templates in Express 3

Currently, I am utilizing Express 3.0 with Jade as the template engine. My goal is to transfer a variable from a template page to the primary layout page. Here's an example of what I'm attempting: Snippet from layout.jade: !!! 5 html(lang=&apos ...

Angular JS causing text alignment issues in table cells when viewed on mobile devices

I have created a web application that requires both desktop and mobile views, utilizing Angular JS. Everything is functioning as expected except for the text alignment within tables. I attempted using <td align="left">, but it did not produce any cha ...

Having trouble locating node_modules/nan on your system?

Trying to globally install this project is proving to be a challenge as I run the command npm i -g. Followed these steps: git clone https://github.com/superflycss/cli cd cli npm i npm i -g However, encountered this result: ole@mki:~/cli$ npm i -g npm W ...

Retrieve a variety of outputs from computed properties in Vue

I have created buttons to select different dates (one for tomorrow and one for next week) with a function that calculates only business days. The 'tomorrow' button should display either 'Tomorrow' or 'Monday', while the ' ...

Combine object attributes to create a new column in a table

Check out this plunker I created. What is the most efficient way to merge the contents of $scope.blacklist into $scope.friends when ng-click="showColumn('Blacklist');" is triggered, and also add a new column named Coming to the table. Ng-App &am ...

"Troubleshooting: React-chartjs-2 Line graph fails to refresh with new data

I have a custom LineChart.js class with the following code snippet: import React from 'react' import {Line} from 'react-chartjs-2'; const data = { labels: ['red'], datasets: [{ label: 'My First Dataset&apo ...

Struggling to locate the xpath for the Following button on Instagram while using Selenium?

Can someone help me with extracting the xpath for a specific button on Instagram, so I can create an automated unfollowing script? Check image here I managed to find the xpath like this: driver.find_element_by_xpath('//div[@class="qF0y9 Igw0E rB ...

Guide on making a Horizontal Flatlist in React Native displaying 2 rows and 3 columns at a time

I am looking to create a Horizontal Scrolling Flatlist with 2 rows and 3 columns visible, pulling dynamic data from an API. Currently, I have successfully implemented a vertical scrolling Flatlist that resembles the following: https://i.sstatic.net/oJJyA. ...

Utilizing Jquery to Pass an Optional Function to Another Function

I am currently working on a function that utilizes AJAX to submit data and then displays a dialog indicating whether the process was successful or not. Everything seems to be functioning smoothly, but I now want to add the capability of passing an addition ...

Strategies for managing dynamically mounted component events

I have a widget defined as: Calculator.vue <template> ... </template> <script> export default { data: function () { return { value: 0, }; }, methods: { ... ...

Show data in a popup using jQuery DataTables and loading content asynchronously via Ajax

I am attempting to display a list in a popup based on an Ajax request. Prior to the Ajax call, the list is contained within the popup. However, after the Ajax request, the list remains on the page instead of inside the popup, and the old list still appears ...