Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:56:07+00:00 2026-06-02T20:56:07+00:00

My app is reaching a point where I must begin optimizing for performance. I’ve

  • 0

My app is reaching a point where I must begin optimizing for performance. I’ve posted some code from my view that I feel can be improved.

In the view, I am treating the first item in the index a certain way and the rest of the items another way. Each time a new item is iterated over, it is being checked (Ruby asks itself.. does this item have an index of 0?)

I feel like performance can be improved if I can stop that behavior by treating the first item special with index.first? and treating the other items another way (without even checking whether they have an index of zero) How can this be done?

  <% @links.each_with_index do |link, index| %>
    <% if link.points == 0 then @points = "?" else @points = link.points %>
    <% end %>
    <% if index == 0 then %>
      <h1> First Item </h1>
    <% else %>
      <h1> Everything else </h1>
    <% end %>
  <% end %>
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:56:10+00:00Added an answer on June 2, 2026 at 8:56 pm

    You can do this non-destructively like so:

    first, *rest = *my_array
    # Do something with 'first'
    rest.each{ |item| … }
    

    …where first will be the first element (or nil if my_array was empty) and rest will always be an array (possibly empty).

    You can get the same results more easily if it’s OK to modify your array:

    # remove the first item from the array and return it
    first = my_array.shift
    # do something with 'first'
    my_array.each{ |item| … }
    

    However, this will only clean up your code; it will make no measurable performance difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app is expanding, but the reqs dictate that there should be only one
I followed this description for debugging native code in an Android app and it
When I point my browser to rails_app.com, I can view rails_app/public/index.html on the EC2
I am attempting to develop an application that makes use of some simple trig
I'm preparing to embark on a web app development that will make little to
I'm working on a project that involves creating a spline from a defined set
I see that the Heap Size is automatically increased as the app needs it,
I'm working on an app that involves the Google Maps API and I'm worrying
We have some unstructured textual data in our app engine datastore. I wanted to
My Android app sends/retrieves data to/from the user's own PC using HTTP and it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.